Gui Programming
Gui Programming
1 Basic Concepts
3 Layout Manager
4 Event Handling
5 Other References
GUI?
Similarities:
Tools provided by Java for developing
interactive GUI applications
Provides GUI components that can be used in
creating Java applications and applets
Simple GUI-Based Input/Output with
JOptionPane
Converting
String
Into
integer
value
3
Output
7
Different JoptionPane
Message Icons
8
JOptionPane Message Icon
(cont)
9
JOptionPane Message Icon
(cont)
1
0
Abstract Window Toolkit (AWT) vs. Swing
MessageBox
InputBox
Create Frame (AWT)
Create Frame (Swing)
CenterFrame
Exercises
TextField
TextArea
Button
List
CheckBox
Choice
Labels
Awt components
• The Abstract Window Toolkit (AWT) is Java's original platform-
independent windowing, graphics, and user-interface widget toolkit.
The AWT is now part of the Java Foundation Classes (JFC) — the
standard API for providing a graphical user interface (GUI) for a Java
program.
• AWT is also the GUI toolkit for a number of Java ME profiles. For
example, Connected Device Configuration profiles require Java
runtimes on mobile telephones to support AWT.
16
Dem
o
FrameWithControl
Swing Control Components
19
Swing Control Components
24
D Importing
Classes
I That will be
S Used in
P
the
L
program
A Declaring three
JLabel Instances
Y
I
T
N
E
XG
T
A
Of the program
Constructor
MI 14
A
G
Output of the
Program
26
Labeling GUI
components
• A typical GUI consists of many components. GUI designers often provide
text stating the purpose of each. Such text is known as a label and is created
with a JLabel—a subclass of JComponent.AJLabel displays read-only text, an
image, or both text and an image. Applications rarely change a label’s
contents after creating it. Following is the Example program:
27
Output of the
program
28
Common GUI Event Types and
• Listener
Following Interfaces
Figure illustrates a hierarchy containing many event
classes from the package java.awt.event. These event types
are used with both AWT and Swing components. Additional
event types that are specific to Swing GUI components are
declared in package javax.swing.event.
29
Jbutto
n
• A button is a component the user clicks to trigger a specific action.
• A Java application can use several types of buttons, including command
buttons, checkboxes, toggle buttons and radio buttons.
Following is the Swing Button Hierarchy:
30
JbuttonExam
ple
actionPerformed
To display a message 20
Dialog Box containing the
label for the button
The user pressed
Jbutton Example
(cont)
Creating
Main
21
Buttons that Maintains
States
• The Swing GUI components contain three types of state
buttons
1. JToggleButton
A toggle button is a button that represents a settings with two
states i.e. on and off.
Toggle buttons look similar to command buttons and display
a graphic or text (or both) to identify the button.
The graphic or button text should remain the same whether
the button is in the on or off state.
The state is indicated by highlighting the background of the
buttons.
Users can click toggle buttons to turn a setting on or off--for
instance, to switch between italic and plain style in selected 22
text.
Button that maintain
States (cont)
2-Check Boxes:
A checkbox is a control that represents a setting or value with an on or
off choice.
The setting of an individual checkbox is independent of other
checkboxes--that is, more than one checkbox in a set can be checked at
any given time.
A check mark within the checkbox indicates that the setting is selected.
The following figure shows both available and unavailable checkboxes in
selected and unselected states.
23
The user clicks a checkbox to switch its setting from off to on, or on to
off. When a checkbox is unavailable, the user cannot change its setting.
Example
JCheckBoxes
Importing the classes
Main Class
25
Buttons that Maintains
States(cont)
3-Radio Buttons
A radio button represents an exclusive choice within a set of related
options.
Within a set of radio buttons, only one button can be on at any given
time.
The following figure shows active radio buttons and inactive radio
buttons in both on and off states.
When users click a radio button, its setting is always set to on. An
inner filled circle within the round button graphic indicates that the
setting is selected. If another button in the set has previously been
selected, its state changes to off.
26
When a radio button is unavailable, users cannot change its setting.
JRadioButton
Example
Creating 4 JRadioButtonobjects
And adding them to JFrame 27
Instantiates ButtonGroup
JRadioButton Example Object radioGroup
(Cont) Using ButtonGroup method add
To associate each of the
JRadioButtons with radio group
39
JRadioButton
(cont)
40
JComboB
ox
A JComboBox, lets the user choose one of several choices, can have two
very different forms.
The default form is the uneditable combo box, which features a button
and
a drop-down list of values.
The second form, called the editable combo box, features a text field with a
small button abutting it. The user can type a value in the text field or click
the button to display a drop-down list.
41
JComboBox
Example
Declaring and initializing array
Icons with four new image
icon objects
It contains the names of the four
Image files that are stored in the
Same directory as the application
43
JLis
t• A JList presents the user with a group of items, displayed in
one or more columns, to choose from.
• Lists can have many items, so they are often put in scroll
panes.
The following figures shows two applications that use
lists.
44
Jlist
(Cont)
Types of Lists:
Single Selection
Only one item can be selected at a time. When the user selects an item, any
previously selected item is deselected first.
45
Jlist
(Cont)
Multiple Interval Selection:
The default. Any combination of items can be selected. The user must
explicitly deselect items.
46
Jlist Example
Creating Jlist object colorJList
48
JSlid
er
• A JSlider component is intended to let the user easily enter a
numeric value bounded by a minimum and maximum value.
The following picture shows an application that uses a slider
to control animation speed:
49
Jslider
Example
51
Jslider Example
(Cont)
52
Layout Managers
BorderLayout Manager
BorderLayout is the default layout of the Frame class.
The BorderLayout layout manager divides the container into
north, south, east, west, and centre regions.
You can place five components or controls in each part.
Java provides the BorderLayout class to apply the border layout to
the components.
The setLayout() method is used for applying border layout to a
container.
You specify the directions for the BorderLayout using the
BorderLayout.NORTH, BorderLayout.SOUTH,
BorderLayout.EAST, BorderLayout.WEST, and
BorderLayout.CENTER constants.
You can use the following constructors to create an instance of the
BorderLayout class:
BorderLayout()
BorderLayout(int h, int v)
Layout Managers
GridLayout Manager
The grid layout is the layout that divides the container
into rows and columns.
The intersection of a row and a column of the grid
layout is called cell.
The GridLayout class of Java enables you to create a grid
layout.
All the components in a grid are of the same size.
You can use the following constructors to create an
instance of the GridLayout class:
• GridLayout()
• GridLayout(int r, int c)
• GridLayout(int r, int c, int h, int v)
Dem
o
FlowLayout
BorderLayout
GridLayout
To be
continued
To be continued….
Reference
s
Java Passion from Sun Microsystems
Java Tutorial from Sun Microsystems
Core Java 2 Volume I Fundamentals 7th
Edition
Java How to Program 6th Edition
Java Swing 2nd Edition
www.niit-vn.c
om
www.niit-vn.com/4rum