Chapter 1
Chapter 1
Chapter 1
Introduction
UI
The user interface (UI) is the space where interactions between
humans and machines occur.
The goal of this interaction is to allow effective operation and
control of the machine from the human end
whilst the machine simultaneously feeds back information that aids
the operators' decision-making process.
except:
Japplet
Jframe
Jwindow
Jdialog
AWT components have Swing analogs
AWT to Swing Mappings
Almost all map by prepending a ‘J’
Examples:
Figure 1.4 Addition program that uses JOptionPane for input and output.
Swing vs. AWT
Look and Feel Observation:
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(AWT) are known as its look-and-feel.
Swing vs. AWT…
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.
An application can even change the look-and-feel during
AWT advantages:
AWT is supported on older, as well as newer, browsers so
Applets written in AWT will run on more browsers.
The Java Micro-Edition, which is used for phones, TV
settop boxes, PDAs, etc, uses AWT, not Swing.
Generally, AWT and Swing …
Class Component (package java.awt) is a super class
that declares the common features of GUI
components in packages java.awt and javax.swing.
Any object that is a Container (package java.awt) can
be used to organize Components by attaching the
Components to the Container.
Containers can be placed in other Containers to
organize a GUI.
Class JComponent (package javax.swing) is a
subclass of Container.
Generally, AWT and Swing …
See figure 1.5. JComponent is the super class of all lightweight
Swing components and declares their common attributes and
behaviors.
Because JComponent is a subclass of Container, all
lightweight Swing components are also Containers.
subclass of JComponent.
A JLabel displays read-only text, an image, or both text and an
image.
Figure 1.6 JLabels with text and icons.
Figure 1.7 Test class for LabelFrame.
Specifying the Layout
When building a GUI, you must attach each GUI component to a
container.
such as a window created with a JFrame.
Also, you typically must decide where to position each GUI
component; known as specifying the layout.
Java provides several layout managers that can help you position
components.
Many IDEs provide GUI design tools in which you can specify
components’ exact sizes and locations in a visual manner by using
the mouse; then the IDE will generate the GUI code for you.
Such IDEs can greatly simplify GUI creation.
Java’s layout managers are used to size and position components.
Specifying the Layout …
With the FlowLayout layout manager, components are placed on a
container from left to right in the order in which they’re added.
When no more components can fit on the current line, they continue
to display left to right on the next line.
If the container is resized, a FlowLayout reflows the components,
possibly with fewer or more rows based on the new container width.
Every container has a default layout, which we’re changing for
LabelFrame to a FlowLayout (line 20, figure 1.6).
Method setLayout is inherited into class LabelFrame indirectly from
class Container.
The argument to the method must be an object of a class that
implements the LayoutManager interface (e.g., FlowLayout). Line
20 creates a new FlowLayout object and passes its reference as the
argument to setLayout.
Specifying the Layout …
The above codes (fig 1.6 and 1.7) used to:
Specifying the Layout
Loading an Image Resource
There are two ways to position a component in a container:
1. Using a predefined layout and allowing the layout to decide where to
position the component.
This is a soft way of positioning a component. If the container changes
its size, the component's position will be adjusted.
But you may not able to get precisely where you want.
2. Specifying the position of the component using the container's
coordinates.
This is a hard way of positioning a component.
You can get precisely where you want the component to be.
But if the container changes its size, the component's position will not be
Specifying the Layout …
Creating and Displaying a LabelFrame Window
Class LabelTest (Fig. 1.7) creates an object of class LabelFrame (line 9), then
specifies the default close operation for the window.
By default, closing a window simply hides the window.
However, when the user closes the LabelFrame window, we would like the
application to terminate.
Line 10 invokes LabelFrame’s setDefaultCloseOperation method (inherited from
class JFrame) with constant JFrame.EXIT_ON_CLOSE as the argument to
indicate that the program should terminate when the window is closed by the user.
This line is important. Without it the application will not terminate when the user
closes the window.
Next, line 11 invokes LabelFrame’s setSize method to specify the width and height
of the window in pixels.
Finally, line 12 invokes LabelFrame’s setVisible method with the argument true to
display the window on the screen.
TextField
Swing provides an extensive collection of classes for working with
text in user interfaces.
In fact, because there's so much provided for working with text,
Swing's creators placed most of it into its own package:
javax.swing.text.
This package's dozens of interfaces and classes (plus the six
concrete component classes in javax.swing) provide a rich set of
text-based models and components complex enough to allow
endless customization yet simple to use in the common case.
The Swing API provides several classes for components that are
either varieties of text fields or that include text fields:
JTextField
JTextArea
JPasswordField. Etc…
Figure 1.8 JTextFields and JPasswordFields
Event and Event Handling
Events come from User Controls
Events are actions usually triggered by the system user or by the actions of
objects; for instance, clicking a button.
When the user types in a JTextField or a JPasswordField, then presses
Enter, an event occurs.
Our next example demonstrates how a program can perform a task in
response to that event.
The techniques shown here are applicable to all GUI components that
generate events.
The application of Figs. 1.8–1.9 below uses classes JTextField and
JPasswordField to create and manipulate four text fields.
When the user types in one of the text fields, then presses Enter, the
application displays a message dialog box containing the text the user typed.
You can type only in the text field that’s “in focus.” When you click a
component, it receives the focus.
Event and Event Handling…
This is important, because the text field with the focus is the one that generates
an event when you press Enter.
In this example, you press Enter in the JPasswordField, the password is
revealed(displayed).
We begin by discussing the setup of the GUI, then discuss the event-handling
code.
Lines 3–9 import the classes and interfaces we use in this example.
Class TextField- Frame extends JFrame and declares three JTextField variables
and a JPasswordField variable (lines 13–16).
Each of the corresponding text fields is instantiated and attached to the
TextFieldFrame in the constructor (lines 19–47).
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.
Event and Event Handling…
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.
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
Event and Event Handling…
Each of these components is a single-line area in which the user can enter
text via the keyboard.
Applications can also display text in a JTextField.
A JPasswordField shows that characters are being typed as the user enters
them, but hides the actual characters with an echo character, assuming that
they represent a password that should remain known only to the user.
Every Input Control (Button, Slider ...) Needs an Event Listener
If you want a control to do something when the user alters the control, you
must have a listener.
Mouse click / Key press, Menu Selection, Timer expiring, Network message
received are all Event Sources.
Some GUI components might care about one of these things happening and
need to react to it.
These components would register themselves with the Event Source, so the
source would tell them when something happens. These are the Event
Event and Event Handling…
To use events, you must import :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
There are several kinds of events. The most common are :
User Control addXXXListener method in listener
Button/JButton
TextField/JTextField addActionListener() actionPerformed(ActionEvent e)
MenuItem/JMenuItem
Slider/Jslider addChangeListener() stateChanged(ChangeEvent e)
CheckBox/JCheckBox addItemListener() itemstateChanged()
key on component addKeyListener() keyPressed(), keyReleased(), keyTyped()
mouseClicked(),
mouseEntered(),
mouse on component addMouseListener()
mouseExited(),
mousePressed(), mouseReleased()
mouseMoved(),
mouse on component addMouseMotionListener()
mouseDragged()
Frame/JFrame addWindowListener() windowClosing(WindowEvent e), ...
Event and Event Handling…
Steps Required to Set Event Handling for a GUI Component
This example should display a message dialog containing the
text from a text field when the user presses Enter in that text
field.
Before an application can respond to an event for a particular
END OF CHAPTER