2. Introduction
• Swing library is an official Java GUI toolkit
released by Sun Microsystems
• Swing is a part of JFC, Java Foundation Classes
• Used to create Graphical user interfaces with
Java.
• It is a collection of packages for creating full
featured desktop applications.
• JFC consists of AWT, Swing, Accessibility, Java 2D,
and Drag and Drop.
• Swing was released in 1997 with JDK 1.2
8. Layout Management
• Java Swing toolkit has two kind of
components
– Container Components
– Children Components
• The containers group children into suitable
layouts
• To create layouts, we use layout managers
10. Absolute Positioning
• If a container holds components whose size is not
affected by the container's size or by font, look-
and-feel, or language changes
• Creating a container without a layout manager
involves the following steps.
1.Set the container's layout manager to null by
calling setLayout(null).
2.Call the Component class's setbounds method for
each of the container's children.
3.Call the Component class's repaint method.
11. Absolute Positioning (contd)
• Creating containers with absolutely positioned
containers can cause problems if the window
containing the container is resized
12. FlowLayout
• The FlowLayout class provides a very simple
layout manager that is used, by default, by the
JPanel objects
• The FlowLayout class puts components in a row,
sized at their preferred size
• If the horizontal space in the container is too
small to put all the components in one row, the
FlowLayout class uses multiple rows
• If the container is wider than necessary for a row
of components, the row is, by default, centered
horizontally within the container
13. FlowLayout (contd)
• To specify that the row is to aligned either to
the left or right, use a FlowLayout constructor
that takes an alignment argument
– public FlowLayout(int align)
• Another constructor of the FlowLayout class
specifies how much vertical or horizontal padding
is put around the components
– public FlowLayout(int align, int hgap, int vgap)
15. BorderLayout
• A BorderLayout object has five areas specified
by the BorderLayout constants:
– PAGE_START
– PAGE_END
– LINE_START
– LINE_END
– CENTER
16. BorderLayout (contd)
• If the window is enlarged, the center area gets
as much of the available space as possible
• Other areas expand only as much as necessary
to fill all available space
• Often a container uses only one or two of the
areas of the BorderLayout object — just the
center, or the center and the bottom.
18. GridLayout
• A GridLayout object places components in a
grid of cells
• Each component takes all the available space
within its cell, and each cell is exactly the
same size
• If the window is resized, the GridLayout object
changes the cell size so that the cells are as
large as possible, given the space available to
the container
21. Introduction
• All GUI applications are event-driven
• An application reacts to different event types
which are generated during its life
• Events are generated mainly by the user of an
application
• But they can be generated by other means as
well. e.g. internet connection, window
manager, timer
22. Introduction (contd)
• In the event model, there are three participants:
– event source
– event object
– event listener
• The Event source is the object whose state changes and
generates Events
• The Event object (Event) encapsulates the state changes in
the event source
• The Event listener is the object that wants to be notified.
Event source object delegates the task of handling an event
to the event listener.
23. Event Object
• When something happens in the application, an
event object is created
• There are several types of events, e.g ActionEvent,
TextEvent, FocusEvent, ComponentEvent etc,
created under specific conditions.
• Event object has information about an event, that
has happened
24. Implementation
• There are several ways, how we can
implement event handling in Java Swing
toolkit
– Anonymous inner class
– Inner class
– Derived class
25. Anonymous Inner Class
• The button is the event source and it will
generate events
closeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
} });
• Here we register an action listener with the
button
• This way, the events are sent to the event
target
• The event target in our case is ActionListener
26. Inner Class
• The listener is defined inside an inner class, which
has a name
ButtonCloseListener listener = new ButtonCloseListener();
closeButton.addActionListener(listener);
• Here we have a non anonymous inner class
class ButtonCloseListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
} }
• The button listener is defined here.
27. Derived Class
• We create a MyButton class, that implements the action
listener
MyButton closeButton = new MyButton("Close");
• Now create the MyButton custom class
class MyButton extends JButton implements ActionListener {
• The MyButton class is extended from the JButton class and
implements the ActionListener interface
• This way, the event handling is managed within the
MyButton class.
addActionListener(this);
• Here we add the action listener to the MyButton class.
28. Adapter Classes
• Time consuming to define all interface
methods
• WindowListener has seven methods
• What if we only want to use one?
• Required to define all methods in interface
• Adapter class implements an interface
• Does anyone recognize a design pattern here?
• Default implementation ({ }, empty body) for
all methods
29. Adapter Classes (contd)
• You then extend adapter class,
• overriding methods for events you care about,
such as windowClosing.
• Has "is a" relationship with interface
• WindowAdapter is a WindowListener
• MouseAdapter is a MouseListener