04 Java Swing
04 Java Swing
04 Java Swing
Lecture 4
Java Swing
CCS2304
What is a GUI Application?
• In such applications, users interact with a set of visual controls
(buttons, labels, text boxes, tool bars, menu items) to make an
application do its required tasks.
2
What is a GUI Application?
• GUI applications offer flexibility, ease of use, familiarity, and
they’re nice to look at.
• When a user clicks the button that says Start Timing, an event
is generated.
Uncheck
5
Creating First Java Swing GUI Application
6
Creating First Java Swing GUI Application
Design
Source
Controls
Form
File View
Properties
& Events
8
Swing’s Features
• Swing is huge (consists of 18 packages of 737 classes as in JDK
1.8) and has great depth.
9
Swing’s Features
• Swing is written in pure Java (except a few classes) and
therefore is 100% portable.
10
Swing’s Features
• Swing supports mouse-less operation (i.e., it can operate
entirely using keyboard).
12
Using Swing API
• If you understood the AWT programming (in particular,
container/component and event-handling), switching over to
Swing (or any otherGraphics packages) is straight-forward.
• Swing's Components:
– Compared with the AWT component classes (in package
java.awt), Swing component classes (in package
javax.swing) begin with a prefix "J“.
Ex: JButton, JTextField, JLabel, JPanel, JFrame, or JApplet.
java.awt javax.swing
Frame JFrame
Panel JPanel
Canvas JPanel
Label JLabel
Button JButton
TextField JTextField
Checkbox JCheckbox
List JList 13
Choice JComboBox
Swing Class Hierarchy
15
Swing Controls
• JFrame control:
– The frame control is the basic ‘container’ for other controls.
It is the framework for a Java project. The title property
establishes the caption information. Every application we
build will start by building a class that extends the Jframe
control.
• JButton control:
– The button control is used to start some action. The text
property is used to establish the caption.
• JLabel control:
– The label control allows placement of formatted text
information on a frame (text property).
16
Swing Controls
• JTextField control:
– The text field control accepts a single line of typed
information from the user (text property).
• JTextArea control:
– The text area control accepts multiple lines of scrollable
typed information (text property).
• JCheckBox control:
– The checkbox control is used to provide a yes or no answer
to a question.
– Moreover, we can make multiple selection.
17
Swing Controls
• JRadioButton control:
– The radio button control is used to select from a mutually
exclusive group of options. You always work with a group of
radio buttons.
• JComboBox control:
– The Combo box controls are very common in GUI
applications. Users can choose an item from a drop-down
list (states, countries, product).
• JList control:
– A list control is like a combo box with the list portion always
visible. Multiple selections can be made with a list control.
18
Swing Controls
• JScroll control:
– A scroll bar control is used to select from a range of values.
The scroll bar is always “buddied” with another control
related to the scroll bar selection.
• JPasswordField control:
– A component that allows the editing of a single line of text
where the view indicates something was typed but does not
show the original characters.
19
JFrame Layout
• A window with a title bar, a resizable border, and possibly a
menu bar.
– A frame is not attached to any other surface.
– It has a content pane that acts as a container.
– The container uses BorderLayout by default.
– A layout manager, an instance of one of the layout classes,
describes how components are placed on a container.
21
GridBagLayout
• Offers the nicest interface appearance.
– The top row is Row 0 and row number increases as you go down the
grid. The left column is Column 0 and column number increases as
you move to the right in the grid.
22
Stopwatch Application – Adding Controls
24
Stopwatch Application – Adding Controls
• Set the position of the controls in the GridBagLayout grid
(inside the constructor):
– First, declare an object of type GridBagConstraints to allow
positioning.
– Place each control in the grid.
– To finalize placement of controls in the frame, execute a pack
method, pack().
25
Stopwatch Application – Adding Event
Methods
• We need to add event methods and their corresponding
listeners to our application.
• There are two ways to add listeners, one for AWT objects and
one for Swing objects. Listeners are added in the frame
constructor code.
• Java event listeners for AWT objects (primarily those for mouse
and keyboard inputs) are implemented using something called
adapters.
Ex: listen for the event when the user closes the window
• The adapter that implements events for the frame is called the
WindowAdapter and it works with the WindowListener.
26
Stopwatch Application – Adding Event
Methods
• In our case, we want to listen for the windowClosing event.
• For Swing components, like the button, label and text field,
event methods (actionPerformed) are added using the
ActionListener.
• For our stopwatch example, we will assume click events for the
three button controls.
27
Stopwatch Application – Adding Event
Methods
• Typically, the control event methods are usually placed after
the constructor method.
28
Stopwatch Application – Adding Event
Methods
• Before writing the control event methods, we should declare
three class level variables.
29
Stopwatch Application – Adding Event
Methods
• Now, we are ready to write the control event methods for the
three buttons.
30
Variables
• Variables must be properly named.
31
Java Data Types
• Each variable is used to store information of a particular type.
• boolean variables can have one of two different values: true or false
(reserved words in Java).
• If a variable stores a whole number (no decimal), there are three data
types available: short, int or long
long -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
32
Java Data Types
• If a variable stores a decimal number, there are two data types: float or
double.
• The double uses twice as much storage as float, providing more precision
and a wider range.
double 3.14159265359
• Single character string variables have a special type, type char, for
character type. Char types are enclosed in single quotes.
Ex: ‘a’
33
Variable Declaration
• To explicitly declare a variable, you must first determine its
scope. Scope identifies how widely disseminated we want the
variable value to be.
34
Arrays
• It provides a way to store a large number of variables under the same
name.
• Each variable, called an element, in an array must have the same data
type, and they are distinguished from each other by an array index
(contained within square brackets).
• We usually declare and create arrays in the same line of code. For
example, to declare an integer array named 'item', with dimension 9, at
the method level, we use:
int[] item = new int[9];
35
Swing Counter Example
36
Any Questions?
37
Java AWT
• Java AWT (Abstract Window Toolkit) is an API to develop
Graphical User Interface (GUI) or windows-based applications in
Java.
38
Swing components are JavaBeans
• JavaBeans is a reusable class code element often applied on
java-based software application programming.
39
The Content-Pane of Swing's Top-Level Container
40