Lect 7
Lect 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.
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.
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.
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.
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
Fields
Modifier and Type Field Description
protected boolean rootPaneCheckingEnabled If true then calls to add and setLayout will
be forwarded to the contentPane.
6
Constructors
Constructor Description
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.
7
protected void frameInit() Called by the
constructors to
init the JFrame
properly.
8
void setLayeredPane(JLayeredPane layeredPane) It sets the
layeredPane
property.
TransferHandler getTransferHandler()
It gets the
transferHandler
property.
Exampl1
Write GUI JAVA program to give the output as shown?
package addaswing;
import javax.swing.JFrame;
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(String s, Icon i, int horizontalAlignment) Creates a JLabel instance with the specified
text, image, and horizontal alignment.
10
Commonly used Methods:
Methods Description
void setText(String text) It defines the single line of text this component
will display.
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.
13
void setIcon(Icon b) It is used to set the specified Icon on the button.
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