0% found this document useful (0 votes)
10 views28 pages

Unit IV-Part II - Swing

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views28 pages

Unit IV-Part II - Swing

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

19CS391 – Programming with Java (Embedded Project)

Thursday, June 27, 2024 1


Swing:

Swing is a lightweight Java graphical user interface (GUI) that is


used to create various applications.
Swing has components which are platform-independent.
It enables the user to create buttons and scroll bars.
Swing includes packages for creating desktop applications in Java.
It is a part of Java Foundation Classes(JFC).

Thursday, June 27, 2024 2


Difference between AWT and Swing:

Thursday, June 27, 2024 3


Difference between AWT and Swing:

Thursday, June 27, 2024 4


Hierarchy of Java Swing classes import javax.swing.*;

Thursday, June 27, 2024 5


Thursday, June 27, 2024 6
Java JButton
The JButton class is used to create a labeled button that has platform
independent implementation.

 The application result in some action when the button is pushed.

It inherits AbstractButton class.

JButton class declaration

public class JButton extends AbstractButton

Thursday, June 27, 2024 7


Commonly used Constructors:

Constructor Description

JButton() It creates a button with no text and icon.

JButton(String s) It creates a button with the specified text.

JButton(Icon i) It creates a button with the specified icon object.

Thursday, June 27, 2024 8


Commonly used Methods of AbstractButton class:

Methods Description
void setText(String s) It is used to set specified text on
button
String getText() It is used to return the text of the
button.
void setEnabled(boolean b) It is used to enable or disable the
button.
void addActionListener(ActionListener a) It is used to add the action listener
to this object.

Thursday, June 27, 2024 9


The adapter classes are found in java.awt.event and javax.swing.event packages.

The Adapter classes with their corresponding listener interfaces are as follows.

Thursday, June 27, 2024 10


Topic

1. Swing Components
 MenuBar and MenuItem
2. Layout Manager
 BorderLayout
 GridLayout
3. Applet

Thursday, June 27, 2024 11


Swing Components

Thursday, June 27, 2024 12


Swing Components
• Text Fields
• Label
• PasswordFiled
• Text Areas
• Buttons
• Check Boxes
• Radio Buttons
• Lists
• Choices
• Scrollbars
• Windows
• Menus – MenuBar, Menu, MenuItem
• Dialog Boxes.

Thursday, June 27, 2024 13


Delegation Event Model in Java

Thursday, June 27, 2024 14


Java JMenuBar & JMenuItem

Thursday, June 27, 2024 15


Layout management - Java LayoutManagers

Thursday, June 27, 2024 16


Layout management - Java LayoutManagers

 The LayoutManagers are used to arrange components in a particular manner.

 LayoutManager is an interface that is implemented by all the classes of layout


managers.

Thursday, June 27, 2024 17


Layout management - Java LayoutManagers

There are the following classes that represent the layout managers:

java.awt.BorderLayout
java.awt.FlowLayout
java.awt.GridLayout
java.awt.CardLayout
java.awt.GridBagLayout
javax.swing.BoxLayout
javax.swing.GroupLayout
javax.swing.ScrollPaneLayout
javax.swing.SpringLayout etc.

Thursday, June 27, 2024 18


Layout management - Java LayoutManagers

Thursday, June 27, 2024 19


Layout management - Java LayoutManagers

Thursday, June 27, 2024 20


Layout management - Java LayoutManagers

Thursday, June 27, 2024 21


Java BorderLayout

The BorderLayout is used to arrange the components in five regions: north,


south, east, west, and center.

Each region (area) may contain one component only.

It is the default layout of a frame or window.

The BorderLayout provides five constants for each region:

public static final int NORTH


public static final int SOUTH
public static final int EAST
public static final int WEST
public static final int CENTER

Thursday, June 27, 2024 22


Java BorderLayout
Constructors of BorderLayout class:

BorderLayout(): creates a border layout but with no gaps between the


components.
BorderLayout(int hgap, int vgap): creates a border layout with the given
horizontal and vertical gaps between the components.

Thursday, June 27, 2024 23


Border Layout Example

Thursday, June 27, 2024 24


Java GridLayout
The Java GridLayout class is used to arrange the components in a
rectangular grid.
One component is displayed in each rectangle.

Constructors of GridLayout class

GridLayout():
creates a grid layout with one column per component in a row.
GridLayout(int rows, int columns):
creates a grid layout with the given rows and columns but no gaps
between the components.
GridLayout(int rows, int columns, int hgap, int vgap):
creates a grid layout with the given rows and columns along with given
horizontal and vertical gaps.
Thursday, June 27, 2024 25
Example 1

Thursday, June 27, 2024 26


Example 2

Thursday, June 27, 2024 27


Example 3

Thursday, June 27, 2024 28

You might also like