Chapter 2 Part One - AWT and Swing-Event
Chapter 2 Part One - AWT and Swing-Event
Code: SWEG2033
Chapter Two
Part One
Objective Components
Button
Objective
• Within this chapter you will be introduced to many of the Java
components for constructing graphical user interfaces (GUIs).
• With the information in this chapter you will be able to develop your
own GUIs.
3
Introduction
• Graphical User Interface (GUI) offers user interaction via some graphical
components.
4
Introduction
• GUI makes it easier for the end user to use an application. It
also makes them interesting.
• There are many sets of visual components and containers for user
interface design in JAVA. But the two are basic :
• AWT (Abstract Window Toolkit) - in package java.awt and
5
Introduction
Class Activity 1
6
Advantages of GUI over CUI
• GUI offers click and execute environment while in CUI every time
we have to enter the command for a task.
7
Class Activity 2 :
8
GUI based Applications
• Navigation System
9
Basic Terminology of GUI
1. Component : is an object having a graphical representation that can
be displayed on the screen and that can interact with the user. For
examples buttons, checkboxes, list and scrollbars of a graphical user
interface.
3. Panel : The Panel is the container that doesn't contain title bar and
menu bars. It can have other components like button, text field etc.
10
Basic Terminology of GUI
4. Window :
• Window is a rectangular area which is displayed on the screen.
11
Basic Terminology of GUI
5. Frame : A Frame is a top-level window with a title and a border. The
size of the frame includes any area designated for the border. Frame
encapsulates window. It and has a title bar, menu bar, borders, and
resizing corners.
12
Basic Terminology of GUI
13
AWT vs. Swing
• The AWT (Abstract Window Toolkit) package is java package designed
for doing windowing interfaces.
2. 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).
3. Behavior: These are events that occur when the user interacts with UI elements.
14
AWT vs. Swing
15
AWT vs. Swing
• The class Component is the abstract base class for the non-menu user
interface controls of AWT. Component represents an object with
graphical representation.
• Following is the declaration for java.awt.Component class:
16
AWT vs. Swing
17
AWT vs. Swing
Methods of AWT
1. protected Component() – Constructor functions.
2. boolean action(Event evt, Object what): should register this
component as ActionListener on component which fires action
events.
See the reference “awt tutorial” book from tutorial point site page number 7-19
18
AWT vs. Swing
• The AWT (Abstract Window Toolkit) package is java package designed for
doing windowing interfaces. Swing can be viewed as an improved version of
the AWT.
• However, Swing did not completely replace the AWT package. Some AWT
classes are replaced by Swing classes, but other AWT classes are needed
when using Swing. We will use classes from both Swing and the AWT.
19
AWT vs. Swing
• AWT is fine for developing simple graphical user interfaces, but not for
developing comprehensive GUI projects.
• Swing components depend less on the target platform and use less of the
native GUI resource.
• For this reason, Swing components that don’t rely on native GUI are referred
to as lightweight components, and AWT components are referred to as
heavyweight components.
20
AWT vs. Swing
• Swing programs use events and event handlers. An event is an object that
acts as a signal to another object known as a listener.
21
AWT vs. Swing (cont’d)
• Graphics and imaging tools, including shape, color, and font classes.
• 100% Pure Java certified versions of the existing AWT component set (Button,
Scrollbar, Label, etc.).
• A rich set of higher-level components (such as tree view, list box, and tabbed panes).
22
Java GUI API
• The GUI API contains classes that can be classified into three
groups:
• component classes
• container classes
• helper classes
24
Java GUI API (cont’d)
25
AWT Hierarchy
26
Useful Methods of Component class
27
Frames
• To create a user interface, you need to create either a frame or an
applet to hold the user-interface components.
• A Frame has:
• a title bar (containing an icon, a title, and the minimize/maximize(restore-
down)/close buttons),
28
Frames
There are two ways to create a frame in AWT.
• By extending Frame class (inheritance)
29
Frames Example by Inheritance
30
AWT Example by Association
31
Frame Swing
Example 1
import javax.swing.JFrame;
public class MyFrame {
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("MyFrame");
frame.setSize(400, 300); // Set the frame size
// Center a frame
frame.setLocationRelativeTo(null); //or .setLocation(300,200)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); // Display the frame
}
}
32
Frame (cont’d)
33
Frame (cont’d)
Adding Components to a Frame
import javax.swing.JFrame;
import javax.swing.JButton;
public class FrameWithButton {
public static void main(String[] args) {
JFrame frame = new JFrame("MyFrame");
// Add a button into the frame
JButton jbtOK = new JButton("OK");
frame.add(jbtOK);
frame.setSize(400, 300);
frame.setLocation(360,360);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
34
Java Label
Adding Lablel
import javax.swing.JFrame;
import javax.swing.JLabel;
public class FrameWithLabel {
public static void main(String[] args) {
JFrame frame = new JFrame("MyFrame");
// Add a lable into the frame
JLabel jLblName = new JLabel(“First Name");
frame.add(jLblName);
frame.setSize(400, 300);
frame.setLocation(360,360);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
35
Java Checkbox
• Clicking on a Checkbox changes its state from "on" to "off" or from "off"
to "on".
36
Java CheckboxGroup
37
Java Label
• The object of Label 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.
38
Java Choice
• The object of Choice class is used to show popup menu of choices.
Choice selected by user is shown on the top of a menu. It inherits
Component class.
39
Java TextArea
• The object of a TextArea class is a multi line region that displays text. It
allows the editing of multiple line text. It inherits TextComponent class.
40
Java Button
• A button object is created in the same way that any other object is created,
but you use the class JButton.
frameObjectVariable.add(endButton);
41
Swing Elements
Element Description
A JColorChooser A JColorChooser provides a pane of controls designed to allow a user to manipulate and
select a color.
JCheckBox A JCheckBox is a graphical component that can be in either an on(true) or off (false)
state.
JRadioButton The JRadioButton class is a graphical component that can be in either an on (true) or off
(false) state. in a group.
JList A JList component presents the user with a scrolling list of text items.
JComboBox A JComboBox component presents the user with a to show up menu of choices.
42
Swing Elements
Element Description
JTextField A JTextField object is a text component that allows editing of a single line of text.
JTextArea A JTextArea object is a text component that allows editing of a multiple lines of text.
ImageIcon A ImageIcon control is an implementation of the Icon interface that paints Icons from
Images.
JScrollbar A Scrollbar control represents a scroll bar component in order to enable the user to select
from a range of values.
JOptionPane JOptionPane provides a set of standard dialog boxes that prompt the users for a value or
informs them of something.
JFileChooser A JFileChooser control represents a dialog window from which the user can select a file.
43
Swing Elements
Element Description
JProgressBar As the task progresses towards completion, the progress bar displays the task's percentage
of completion.
JSlider A JSlider lets the user graphically select a value by sliding a knob within a bounded
interval.
JSpinner A JSpinner is a single line input field that lets the user select a number or an object value
from an ordered sequence.
44
Redding Assignments
45
AWT and Swing
Part Two