Ch1 AWT and Swing
Ch1 AWT and Swing
Introduction
A graphical user interface (GUI) presents a user-
friendly mechanism for interacting with an application. A
GUI gives an application a distinctive “look and feel.”
GUIs are built from GUI components. These are
sometimes called controls or widgets—short for window
gadgets. A GUI component is an object with which the
user interacts via the mouse, the keyboard or another
form of input, such as voice recognition. In this chapter
you’ll learn about many of Java’s so-called Swing GUI
components from the javax.swing package. We cover
other GUI components as they’re needed throughout the
rest of the course.
2
Introduction (cont…)
Many IDEs provide GUI design tools with which
you can specify a component’s exact size and
location in a visual manner by using the mouse.
The IDE generates the GUI code for you. Though
this greatly simplifies creating GUIs, each IDE
generates this code differently. For this reason, we
try to use different IDEs.
3
Simple GUI-Based Input/Output with
JOptionPane
The applications in basic programming and object oriented
programming in java display text in the command window
and obtain input from the command window. Most
applications you use in advanced programming in java use
windows or dialog boxes (also called dialogs) to interact
with the user. For example, an e-mail program allows you
to type and read messages in a window the program
provides. Dialog boxes are windows in which programs
display important messages to the user or obtain
information from the user. Java’s JOptionPane class
(package javax.swing) provides prebuilt dialog boxes for
both input and output. These are displayed by invoking
static JOptionPane methods. 4
Overview of Swing Components
Though it’s possible to perform input and output using the
JOptionPane dialogs, most GUI applications require more elaborate
user interfaces. The remainder of this chapter discusses many GUI
components that enable application developers to create robust GUIs.
The following Figure lists several basic Swing GUI components that
we discuss.
5
Swing vs. AWT
There are actually two sets of Java GUI components. In
Java’s early days, GUIs were built with components from the
Abstract Window Toolkit (AWT) in package java.awt.
These look like the native GUI components of the platform
on which a Java program executes. For example, a Button
object displayed in a Java program running on Microsoft
Windows looks like those in other Windows applications.
On Apple Mac OS X, the Button looks like those in other
Mac applications. Sometimes, even the manner in which a
user can interact with an AWT component differs between
platforms. The component’s appearance and the way in
which the user interacts with it are known as its look-and-
feel 6
Text Fields and an Introduction to Event
Handling with Nested Classes
Normally, a user interacts with an application’s GUI to indicate
the tasks that the application should perform. For example,
when you write an e-mail in an e-mail application, clicking the
Send button tells the application to send the e-mail to the
specified e-mail addresses. GUIs are event driven. When the
user interacts with a GUI component, the interaction—known
as an event—drives the program to perform a task. Some
common user interactions that cause an application to perform
a task include clicking a button, typing in a text field, selecting
an item from a menu, closing a window and moving the mouse.
The code that performs a task in response to an event is called
an event handler, and the overall process of responding to
events is known as event handling.
7
Cont…
Let’s consider two other GUI components that can
generate events— JtextFields and JPasswordFields
(package javax.swing). Class JTextField extends class
JTextComponent (package javax.swing.text), which
provides many features common to Swing’s text-based
components. Class JPasswordField extends JTextField
and adds methods that are specific to processing
passwords.
8
JButton
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. As you can see, all the button types are
subclasses of AbstractButton (package javax.swing), which
declares the common features of Swing buttons. In this
section, we concentrate on buttons that are typically used to
initiate a command.
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
GUI can have many JButtons, but each button label should be
unique in the portion of the GUI that’s currently displayed.
9
Buttons That Maintain State
The Swing GUI components contain 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. A JRadioButton is different from a
JCheckBox in that normally several JRadioButtons are
grouped together and are mutually exclusive—only one in
the group can be selected at any time, just like the
buttons on a car radio.
10
JComboBox; Using an Anonymous Inner
Class for Event Handling
A combo box (sometimes called a drop-down list)
enables the user to select one item from a list. Combo
boxes are implemented with class JComboBox,
which extends class JComponent.JComboBoxes
generateItemEvents just as JCheckBoxes and
JRadioButtons do.
11
JList
A list displays a series of items from which the user may
select one or more items. Lists are created with class
JList, which directly extends class JComponent. Class
JList supports single-selection lists (which allow only
one item to be selected at a time) and multiple-
selection lists (which allow any number of items to be
selected). In this section, we discuss single-selection
lists.
12
Introduction to Layout Managers
Layout managers arrange GUI components in a
container for presentation purposes. You can use the
layout managers for basic layout capabilities instead of
determining every GUI component’s exact position and
size. This functionality enables you to concentrate on
the basic look-and-feel and lets the layout managers
process most of the layout details. All layout managers
implement the interface LayoutManager (in package
java.awt). ClassContainer’ssetLayout method takes an
object that implements the LayoutManager interface as
an argument.
13
Cont…
There are basically three ways for you to arrange
components in a GUI:
1. Absolute positioning: This provides the greatest level
of control over a GUI’s appearance.
2. Layout managers: Using layout managers to position
elements can be simpler and faster than creating a
GUI with absolute positioning, but you lose some
control over the size and the precise positioning of
GUI components.
3. Visual programming in an IDE: IDEs provide tools
that make it easy to create GUIs.
14
JTextArea
A JTextArea provides an area for manipulating multiple
lines of text. Like class JTextField, JTextArea is a
subclass of JTextComponent, which declares common
methods for JTextFields,JTextAreas and several other
text-based GUI components.
15
Thank you
17