Lecture 4
Lecture 4
7.1. Introduction
7.2. GUI components
7.3. Layout Management
7.4. Event handling
7.5. Deployment
A graphical user interface (GUI) presents a user-
friendly mechanism for interacting with an application.
◦ Pronounced “GOO-ee”
◦ Gives an application a distinctive “look-and-feel.”
◦ Consistent, intuitive user-interface components give users a
sense of familiarity
◦ Learn new applications more quickly and use them more
productively.
Built from GUI components.
◦ Sometimes called controls or widgets—short for window gadgets.
User interacts via the mouse, the keyboard or another
form of input, such as voice recognition.
IDEs
◦ Provide GUI design tools to specify a component’s size, location
and other attributes in a visual manner by using the mouse,
keyboard and drag-and-drop.
◦ Generate the GUI code for you.
◦ Greatly simplify creating GUIs, but each IDE has different
capabilities and generates different code.
Example of a GUI: SwingSet3 application (Fig. 12.1)
http://www.oracle.com/technetwork/java/javase/
downloads/index.html
title bar at top contains the window’s title.
menu bar contains menus (File and View).
In the top-right region of the window is a set of buttons
◦ Typically, users press buttons to perform tasks.
In the GUI Components area of the window is a combo
box;
◦ User can click the down arrow at the right side of the box to select
from a list of items.
7.2. GUI components
Most applications use windows or dialog boxes (also called
dialogs) to interact with the user.
JOptionPane (package javax.swing) provides
prebuilt dialog boxes for input and output
◦ Displayed via static JOptionPane methods.
Figure 12.2 uses two input dialogs to obtain integers from
the user and a message dialog to display the sum of the
integers the user enters.
JOptionPane static method
showInputDialog displays an input dialog, using
the method’s String argument as a prompt.
◦ The user types characters in the text field, then clicks
OK or presses the Enter key to submit the String to
the program.
◦ Clicking OK dismisses (hides) the dialog.
◦ Can input only Strings. Typical of most GUI
components.
◦ If the user clicks Cancel, returns null.
◦ JOptionPane dialog are dialog—the user cannot
interact with the rest of the application while dialog is
displayed.
Converting Strings to int Values
◦ Integer class’s static method parseInt converts its
String argument to an int value and might throw a
NumberFormatException.
Message Dialogs
◦ JOptionPane static method showMessageDialog
displays a message dialog.
◦ The first argument helps determine where to position the
dialog.
If null, the dialog box is displayed at the center of your screen.
◦ The second argument is the message to display.
◦ The third argument is the String that should appear in the
title bar at the top of the dialog.
◦ The fourth argument is the type of message dialog to display.
Swing GUI components located in package
javax.swing.
Abstract Window Toolkit (AWT) in package java.awt is
another set of GUI components in Java.
Together, the appearance and the way in which the user interacts
with the application are known as that application’s look-and-
feel.
Swing GUI components allow you to specify a uniform look-
and-feel for your application across all platforms or to use each
platform’s custom look-and-feel.
Most Swing components are not tied to actual GUI
components of the underlying platform.
◦ Known as lightweight components.
AWT components are tied to the local platform and are
called heavyweight components, because they rely on the
local platform’s windowing system to determine their
functionality and their look-and-feel.
Class Component (package java.awt) declares
many of the attributes and behaviors common to the
GUI components in packages java.awt and
javax.swing.
Most GUI components extend class Component
directly or indirectly.
Class Container (package java.awt) is a
subclass of Component.
Components are attached to Containers so that
they can be organized and displayed on the screen.
Any object that is a Container can be used to
organize other Components in a GUI.
Because a Container is a Component, you can
place Containers in other Containers to help
organize a GUI.
Class JComponent (package javax.swing) is a
subclass of Container.
JComponent is the superclass of all lightweight
the component.
Before an application can respond to an event for a
particular GUI component, you must perform several
coding steps:
Create a class that represents the event handler.
Implement an appropriate interface, known as an event-
listener interface, in the class from Step 1.
Indicate that an object of the class from Steps 1 and 2
should be notified when the event occurs. This is known
as registering the event handler.
All the classes discussed so far were so-called top-level
classes—that is, they were not declared inside another
class.
Java allows you to declare classes inside other classes
for events.
The component with which the user interacts is the event
source.
ActionEvent method getSource (inherited from class
EventObject) returns a reference to the event source.
ActionEvent method getActionCommand obtains
the text the user typed in the text field that generated the
event.
JPasswordField method getPassword returns the
password’s characters as an array of type char.
A button is a component the user clicks to trigger a
specific action.
Several types of buttons
◦ command buttons
◦ checkboxes
◦ toggle buttons
◦ radio buttons
Button types are subclasses of AbstractButton
(package javax.swing), which declares the
common features of Swing buttons.
A command button generates an ActionEvent when
the user clicks it.
Command buttons are created with class JButton.
The text on the face of a JButton is called a button
label.
A JButton can display an Icon.
A JButton can also have a rollover Icon
◦ displayed when the user positions the mouse over the JButton.
◦ The icon on the JButton changes as the mouse moves in and out
of the JButton’s area on the screen.
AbstractButton method setRolloverIcon
specifies the image displayed on the JButton when the
user positions the mouse over it.
Three types of state buttons—JToggleButton,
JCheckBox and JRadioButton—that have on/off
or true/false values.
Classes JCheckBox and JRadioButton are
subclasses of JToggleButton.
JRadioButtons are grouped together and are
mutually exclusive—only one in the group can be
selected at any time
JTextField method setFont (inherited by
JTextField indirectly from class Component) sets the
font of the JTextField to a new Font (package
java.awt).
String passed to the JCheckBox constructor is the
checkbox label that appears to the right of the JCheckBox
by default.
When the user clicks a JCheckBox, an ItemEvent occurs.
◦ Handled by an ItemListener object, which must implement method
itemStateChanged.
An ItemListener is registered with method
addItemListener.
JCheckBox method isSelected returns true if a
JCheckBox is selected.
Radio buttons (declared with class JRadioButton)
are similar to checkboxes in that they have two states
—selected and not selected (also called deselected).
Radio buttons normally appear as a group in which
only one button can be selected at a time.
Used to represent mutually exclusive options.
The logical relationship between radio buttons is
maintained by a ButtonGroup object (package
javax.swing), which organizes a group of buttons
and is not itself displayed in a user interface.
ButtonGroup method add associates a
JRadioButton with the group.
If more than one selected JRadioButton object is