0% found this document useful (0 votes)
7 views15 pages

Lect 7

The document provides an overview of Java Swing, a part of Java Foundation Classes (JFC) used for creating window-based applications with platform-independent components. It details the hierarchy of Swing classes, commonly used UI elements like JLabel, JButton, and JFrame, and their functionalities. Additionally, it includes example Java programs demonstrating the creation of a JFrame and usage of JLabel and JButton.

Uploaded by

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

Lect 7

The document provides an overview of Java Swing, a part of Java Foundation Classes (JFC) used for creating window-based applications with platform-independent components. It details the hierarchy of Swing classes, commonly used UI elements like JLabel, JButton, and JFrame, and their functionalities. Additionally, it includes example Java programs demonstrating the creation of a JFrame and usage of JLabel and JButton.

Uploaded by

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

Lecture 7

Outline
• Java Swing
• What is JFC ?
• Hierarchy of Java Swing classes
• SWING - Controls
• SWING UI Elements
• Swing JFrame
• Swing JLabel
• Swing JButton

1
Java Swing
is a part of Java Foundation Classes (JFC) that is used to create window-based applications. It is
built on the top of AWT (Abstract Windowing Toolkit) API (Application Programming Interface)
and entirely written in java.

Unlike AWT, Java Swing provides platform-independent and lightweight components.

The javax.swing package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.

What is JFC ?

The Java Foundation Classes (JFC) are a set of GUI (Graphical User Interface) components which
simplify the development of desktop applications.

Hierarchy of Java Swing classes


The hierarchy of java swing API is given below:

2
SWING - Controls
Every user interface considers the following three main aspects −
• UI Elements − These are the core visual elements the user eventually sees and interacts
with. GWT provides a huge list of widely used and common elements varying from basic
to complex.
• Layouts − They define how UI elements should be organized on the screen and provide a
final look and feel to the GUI (Graphical User Interface). This part will be covered
• Behavior − These are the events which occur when the user interacts with UI elements.
This part will be covered in the Event Handling chapter.

Every SWING controls inherits properties from the following Component class hierarchy.

S.No. Class & Description

Component

1 A Component is the abstract base class for the non menu user-interface
controls of SWING. Component represents an object with graphical
representation

Container
2
A Container is a component that can contain other SWING components

JComponent
A JComponent is a base class for all SWING UI components. In order to
3 use a SWING component that inherits from JComponent, the component
must be in a containment hierarchy whose root is a top-level SWING
container

3
SWING UI Elements
Following is the list of commonly used controls while designing GUI using SWING.

S.No. Class & Description

JLabel
1
A JLabel object is a component for placing text in a container.

JButton
2
This class creates a labeled button.

JColorChooser
3 A JColorChooser provides a pane of controls designed to allow a user to
manipulate and select a color.

JCheck Box
4 A JCheckBox is a graphical component that can be in either an on (true)
or off (false) state.

JRadioButton
5 The JRadioButton class is a graphical component that can be in either
an on (true) or off (false) state. in a group.

JList
6
A JList component presents the user with a scrolling list of text items.

JComboBox
7 A JComboBox component presents the user with a to show up menu of
choices.

JTextField
8 A JTextField object is a text component that allows for the editing of a single
line of text.

4
JPasswordField
9
A JPasswordField object is a text component specialized for password entry.

JTextArea
10 A JTextArea object is a text component that allows editing of a multiple lines
of text.

ImageIcon
11 A ImageIcon control is an implementation of the Icon interface that paints
Icons from Images

JScrollbar
12 A Scrollbar control represents a scroll bar component in order to enable the
user to select from range of values.

JOptionPane
13 JOptionPane provides set of standard dialog boxes that prompt users for a
value or informs them of something.

JFileChooser
14 A JFileChooser control represents a dialog window from which the user can
select a file.

JProgressBar
15 As the task progresses towards completion, the progress bar displays the
task's percentage of completion.

JSlider
16 A JSlider lets the user graphically select a value by sliding a knob within a
bounded interval.

JSpinner
17 A JSpinner is a single line input field that lets the user select a number or an
object value from an ordered sequence.

5
1. Swing JFrame
The javax.swing.JFrame class is a type of container which inherits the java.awt.Frame class.
JFrame works like the main window where components like labels, buttons, textfields are added
to create a GUI.

Unlike Frame, JFrame has the option to hide or close the window with the help of
setDefaultCloseOperation(int) method.

Nested Class
Modifier and Class Description
Type

protected class JFrame.AccessibleJFrame This class implements accessibility support for


the JFrame class.

Fields
Modifier and Type Field Description

protected accessibleContext The accessible context property.


AccessibleContext

static int EXIT_ON_CLOSE The exit application default window close


operation.

protected JRootPane rootPane The JRootPane instance that manages the


contentPane and optional menuBar for this
frame, as well as the glassPane.

protected boolean rootPaneCheckingEnabled If true then calls to add and setLayout will
be forwarded to the contentPane.

6
Constructors
Constructor Description

JFrame() It constructs a new frame that is initially


invisible.

JFrame(GraphicsConfiguration gc) It creates a Frame in the specified


GraphicsConfiguration of a screen device and a
blank title.

JFrame(String title) It creates a new, initially invisible Frame with


the specified title.

JFrame(String title, GraphicsConfiguration gc) It creates a JFrame with the specified title and
the specified GraphicsConfiguration of a screen
device.

Useful Methods
Modifier and Method Description
Type

protected void addImpl(Component comp, Object constraints, int index) Adds the
specified child
Component.

protected createRootPane() Called by the


JRootPane constructor
methods to
create the
default
rootPane.

7
protected void frameInit() Called by the
constructors to
init the JFrame
properly.

void setContentPane(Containe contentPane) It sets the


contentPane
property

static void setDefaultLookAndFeelDecorated(boolean Provides a hint


defaultLookAndFeelDecorated) as to whether
or not newly
created
JFrames should
have their
Window
decorations
(such as
borders,
widgets to
close the
window,
title...)
provided by
the current
look and feel.

void setIconImage(Image image) It sets the


image to be
displayed as
the icon for
this window.

void setJMenuBar(JMenuBar menubar) It sets the


menubar for
this frame.

8
void setLayeredPane(JLayeredPane layeredPane) It sets the
layeredPane
property.

JRootPane getRootPane() It returns the


rootPane
object for this
frame.

TransferHandler getTransferHandler()
It gets the
transferHandler
property.

Exampl1
Write GUI JAVA program to give the output as shown?

package addaswing;

import javax.swing.JFrame;

public class Addaswing {

public static void main(String[] args) {


// TODO code application logic here
JFrame frame = new JFrame("JFrame Example ");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

9
2. Swing JLabel
The object of JLabel class is a component for placing text in a container. It is used to display a
single line of read only text. The text can be changed by an application but a user cannot edit it
directly. It inherits JComponent class.

JLabel class declaration


Let's see the declaration for javax.swing.JLabel class.

public class JLabel extends JComponent implements SwingConstants, Accessible

Commonly used Constructors:


Constructor Description

JLabel() Creates a JLabel instance with no image and


with an empty string for the title.

JLabel(String s) Creates a JLabel instance with the specified


text.

JLabel(Icon i) Creates a JLabel instance with the specified


image.

JLabel(String s, Icon i, int horizontalAlignment) Creates a JLabel instance with the specified
text, image, and horizontal alignment.

10
Commonly used Methods:
Methods Description

String getText() It returns the text string that a label displays.

void setText(String text) It defines the single line of text this component
will display.

void setHorizontalAlignment(int alignment) It sets the alignment of the label's contents


along the X axis.

Icon getIcon() It returns the graphic image that the label


displays.

int getHorizontalAlignment() It returns the alignment of the label's contents


along the X axis.

Exampl2
Write GUI JAVA program to give the output as shown?

11
package addaswing2;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Addaswing2 {
public static void main(String[] args) {
// TODO code application logic here
JFrame frame= new JFrame("Label Example2 ");
JLabel label;
label=new JLabel("JLabel of addaswing");
label.setBounds(50,50, 130,30);
frame.add(label);
frame.setSize(400,400);
frame.setLayout(null);
frame.setVisible(true);
}
}

12
3. Swing JButton
The Jbutton which is implementation of push button is used to create labelled button. On
pressing it generate an event. The JButton inherits AbstractButton and implements Accessible.

JButton class declaration


Let's see the declaration for javax.swing.JButton class.

public class JButton extends AbstractButton implements Accessible

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.

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.

13
void setIcon(Icon b) It is used to set the specified Icon on the button.

Icon getIcon() It is used to get the Icon of the button.

void setMnemonic(int a) It is used to set the mnemonic on the button.

void addActionListener(ActionListener a) It is used to add the action listener to this object.

Exampl3
Write GUI JAVA program to give the output as shown?

package addaswing3;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Addaswing3 {
public static void main(String[] args) {
// TODO code application logic here
JFrame f = new JFrame("Button Example");
JButton b = new JButton("Click");
b.setBounds(130, 100, 100, 30);
f.add(b);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}
}

14

You might also like