Professional Documents
Culture Documents
Module 13 Swing
Module Objectives
At the end of this module, participants will be
able to:
Create custom GUI using the Java Swing API
Listen for and handle events from GUI
components
Agenda
AWT/Swing Framework
Creating and Displaying a Frame
Adding Components to a Container
Using Layout Managers
Creating Complex Layouts
AWT
Stands for Abstract Window Toolkit.
First generation windowing, graphics and user interface toolkit
for Java.
Acted as a thin abstraction layer over the platforms native user
interface.
GUI written using AWT will change its appearance to use the
native platforms look and feel.
Swing
Second generation windowing toolkit for Java.
Draws its own widgets and user interface elements instead of
relying on the native windowing system.
Has a greater amount of features and capabilities than AWT at
the cost of performance, complexity, and steeper learning curve.
Has the ability to alter its look-and-feel independent of the
underlying native windowing system.
Part of the J2SE since 1.2.
Swing API
The package javax.swing and its sub-packages contains the
necessary class files for the different Swing components, event
handlers, and various utilities.
Many elements from the AWT libraries under the package
java.awt are still in use by Swing.
Classes that belong to Swing typically starts with J to
differentiate them their AWT counterparts.
Ex: Frame (AWT) vs JFrame (Swing)
A Simple Frame
package sef.module14.sample;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Container;
public class SimpleJFrame extends JFrame{
private JLabel firstLabel;
public SimpleJFrame(){
firstLabel= new JLabel("First");
Container pane = getContentPane();
pane.add(firstLabel );
setLocation(300,300);
setSize(100,100);
setVisible(true);
}
public static void main(String arg[]){
SimpleJFrame frame = new SimpleJFrame();
}
Swing Imports
The following two lines import two classes from the Swing library
for use in our application.
import javax.swing.JFrame;
import javax.swing.JLabel;
10
11
12
13
14
Using BorderLayout
The BorderLayout Manager divides the container into 5 distinct
zones named NORTH, SOUTH, EAST, WEST and CENTER.
When a component is added to the container, the code must
also specify where the component is to be placed.
The size of the component added will depend on the dimensions
of the container.
BorderLayout is the default Layout of the JFrame content pane.
** Refer to the BorderLayoutFrame.java sample code
16
17
18
Combining Layouts
The JPanel class is a container that is designed to be placed
inside other containers.
JPanels can set their own layouts independently from the parent
container layout.
JPanels can be placed inside other JPanels.
19
20
Event Handlers
Event handlers are used to add interactivity to GUIs.
All components are event sources that generate events like
mouse clicks and movement, keyboard input, etc.
Event listeners/handler are objects that want to be informed of
events generated by event sources and execute behavior when
an event is generated.
Event listeners are interfaces that are implemented by classes.
Each listener interface represents different possible kinds of
events.
21
22
Registering Listeners
Identify the components that will act as the source of the events.
Identify the kind of events that these components might
generate in order to determine what kind of listener interfaces to
use.
Implement the listener methods.
Register an instance of the event handler classes to the
appropriate event sources.
23
24
25
Activity
1)
2)
3)
4)
5)
6)
Hint:
JPanels by default are transparent. You can use their setOpaque() method to
change this.
You can use JSlider to render a slider and ChangeListener to monitor events.
26