Lab 5 Layout Managers + Handling Item Events
Lab 5 Layout Managers + Handling Item Events
Introduction
This lab presents the use of Layout Managers for organizing GUI components in a
frame. Layout Managers implemented in this lab session are FlowLayout and
BorderLayout. In addition, demonstrations of how to implement ItemListener is
provided.
Objectives
Tools/Software Requirement
1. NetBeans
Description
A. JFrame
A frame is a window in which different components/control are added and arranged
on it. There are different ways to create a JFrame, in this lab manual the following
way is adopted:
4. JFrame’s constructor uses its String argument as the text in the window’s title
bar. Super constructor can be invoked with title sent as its String argument to
set the title of the JFrame.
B. Layout Managers
1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. javax.swing.BoxLayout
5. java.awt.CardLayout
6. java.awt.GridBagLayout
7. javax.swing.GroupLayout
8. javax.swing.ScrollPaneLayout
9. javax.swing.SpringLayout etc.
B.1 BorderLayout
The BorderLayout is used to arrange the components in five regions: north, south,
east, west and center. Each region (area) may contain one component only. It is the
default layout of frame or window. The BorderLayout provides five constants for
each region:
1. public static final int NORTH
2. public static final int SOUTH
3. public static final int EAST
4. public static final int WEST
5. public static final int CENTER
Constructor Description
BorderLayout() Creates a border layout but with no gaps between the
components.
BorderLayout(int hgap, int vgap) Creates a border layout with the given horizontal and
vertical gaps between the components.
LayoutApp.java
Output
B.2 FlowLayout
The FlowLayout is used to arrange the components in a line, one after another (in a
flow). It is the default layout of applet or panel. The FlowLayout provides five
constants:
1. public static final int LEFT
2. public static final int RIGHT
3. public static final int CENTER
4. public static final int LEADING
5. public static final int TRAILING
Constructor Description
FlowLayout() Creates a flow layout with centered alignment
and a default 5 unit horizontal and vertical gap.
FlowLayout( int align) Creates a flow layout with the given alignment
and a default 5 unit horizontal and vertical gap.
FlowLayout( int align, int hgap, Creates a flow layout with the given alignment
int vgap) and the given horizontal and vertical gap.
LayoutApp.java
Output
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. 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.
JCheckBox
JRadioButton ItemEvent ItemListerner itemStateChanged() addItemListerner()
JComboBox
EventHandlingAppII.java
Output
This application should enable the user to select one or more courses and
choose one major. After selecting the major, the application should get the
user selections and display a JOptionPane message dialog with the user
selections. See below:
boolean isSelected() Returns the state of the button. True if the toggle
button is selected, false if it's not.
(Defined in AbstractButton)
Example C.1
MyJFrame.java
EventHandlingAppII.java
Output