AOOP CH - 4 GUI-1
AOOP CH - 4 GUI-1
By Nahom.G
C H A P T E R F O U R : JAVA G U I A N D J D B C
INTRODUCTION
Swing and
JavaFX.
… DEVELOPING GUI WITH JAVA
1. Abstract Windowing Toolkit (AWT) API was introduced in JDK 1.0. Most of the
AWT components have become absolute and should be replaced by newer Swing
components.
2. Swing API, a much more comprehensive set of graphics libraries that enhances the
AWT, it was introduced as part of Java Foundation Classes (JFC) after the release of
JDK 1.1.
JFC consists of Swing, Java2D, Accessibility, Internationalization, and Pluggable
Look-and Feel Support APIs. JFC has been integrated into core Java since JDK 1.2.
3. The latest JavaFX, which was integrated into JDK 8, is meant to replace Swing.
CONTAINERS AND COMPONENTS
For example,
GUI components are also called controls (e.g., Microsoft ActiveX Control), widgets (e.g.,
Eclipse's Standard Widget Toolkit, Google Web Toolkit), which allow users to interact with (or
control) the application.
AWT CONTAINER CLASSES
Top-Level Containers: Frame, Dialog and Applet
Each GUI program has a top-level
container. The commonly-used top-level
containers in AWT are Frame, Dialog and
Applet:
A Frame provides the "main window" for
your GUI application.
It has a title bar (containing an icon, a
title, the minimize,
maximize/restoredown and close buttons),
an optional menu bar, and the content
display area.
To write a GUI program, we typically
start with a subclass extending from
java.awt.
Frame to inherit the main window as
import java.awt.Frame; // Using Frame class in package java.awt
// This subclass inherits all properties from Frame, e.g., title, icon, buttons, content-pane
new MyGUIProgram(); } }
An AWT Dialog is a "pop-up
window" used for interacting with the
users.
A Dialog has a title-bar (containing an
icon, a title and a close button) and a
content display area, as illustrated.
An AWT Applet (in package
java.applet) is the top-level container
for an applet, which is a Java program
running inside a browser.
SECONDARY CONTAINERS: PANEL AND SCROLL PANE
Panel: a rectangular box used to layout a set of related GUI components in pattern
such as grid or flow.
Scroll Pane: provides automatic horizontal and/or vertical scrolling for a single
child component.
HIERARCHY OF THE AWT CONTAINER CLASSES
COMPONENTS
Components- are GUI entities with which you can allow the users of your application to use
application easily.
In other word, an object with which the users interacts with the application via input forms
like keyboard, mouse..).
Container
Container : Container is a GUI that holds a set of components.
- JFrame
- JApplet
- JPanel
LAYOUT MANAGERS
Programmers can use the layout managers for basic layout capabilities instead
of determining the exact position and size of every GUI component.
All layout managers implement the interface Layout Manager (in package
java.awt).
FLOW LAYOUT CLASS
Flow Layout is the simplest layout manager.
GUI components are placed on a container from left to right in the order in
which they are added to the container.
When the edge of the container is reached, components continue to display on
the next line.
Flow Layout Class allows GUI components to be left aligned, centered (the
default) and right aligned.
Eg: Jframe frame=new Jframe();
frame.setLayout(new FlowLayout());
BORDER LAYOUT CLASS
The Border Layout manager arranges components into five regions: NORTH, SOUTH, EAST,
WEST and CENTER.
A Border Layout limits a Container to containing at most five components—one in each region.
NORTH
SOUTH
Eg: frame.setLayout(new BorderLayout());
frame.add(Btn1,BorderLayout.NORTH);
frame.add(Btn2,BorderLayout.WEST);
. . .
. . .
SAMPLE CODE
PUT
T
OU
GRID LAYOUT CLASS
The GridLayout layout manager divides the container into a grid.
Components are added to a GridLayout starting at the top-left cell of the grid
and proceeding left to right until the row is full.
Then the process continues left to right on the next row of the grid, and so on.
Eg: frame.setLayout(new GridLayout (2,4));
JTOOL TIP CLASS:
JToolTip Class: The Swing components support the ability to display brief pop-
up messages when the cursor rests over them.
Eg: nameText.setHorizontalAlignment(JTextField.RIGHT);
JCHECKBOX CLASS
The JCheckBox class represents the toggle component.
It is JToggleButton subclass.
Displays a check box icon next to the text label for a two-state
option.
The Icon uses an optional check mark to show the current state of
the object.
JCheckBox inherits all properties from AbstractButton Class. (text,
icon, alignment, text position, selected, mnemonics ).
JCHECKBOX CLASS …
– Selection: this is can either help to check the selection status of a JCheckBox
object or it is used to set icon options when the instance is selected.
JCHECKBOX PROPERTIES…
• Eg. wolloCheckedBox.isSelected( );
– //checks the selection status and returns true if selected
• Eg. emptyCheckedBox.setSelectedIcon(Icon icon);
– //sets the icon when the instance is selected.