Professional Documents
Culture Documents
SWING and AWT
SWING and AWT
1
Introduction
2
Introduction cont’d
3
Event handling
4
Swing and AWT
5
Swing and AWT cont’d
6
Swing and AWT cont’d
8
Swing and AWT cont’d
Although Swing eliminates a number of limitations
inherent in the AWT, Swing does not replace it.
9
The swing component class hierarchy
10
The swing component class hierarchy cont’d
Fig. Hierarchy of the
Swing components
without AWT counterparts
11
The swing component class hierarchy cont’d
Fig. Swing
components present
in AWT (but with
extended capabilities)
12
The Containment hierarchy
A containment hierarchy is:
A tree of components that has a top-level container as
its root.
The hierarchy that contains all of the Swing
components that appear inside the top-level container.
Each top-level container has a content pane that contains
(directly or indirectly) visible components in that top-level
container's GUI.
13
The Containment hierarchy cont’d
All Swing GUI applications make use of a
containment hierarchy.
Is more or less unrelated to (or at least different from)the
Swing components' position on the class hierarchy.
The containment hierarchy, from top to bottom, is as follows:
Top-level Container(s)
Intermediate-level Container(s)
Atomic Component(s)
14
The Containment hierarchy cont’d
Top-level Containers
Exist mainly to provide a place for other Swing
components to paint themselves.
Swing provides four top-level container classes:
15
The Containment hierarchy cont’d
Intermediate Containers
Its purpose is to simplify positioning atomic
components like buttons and labels.
Play a more visible, interactive role in a program's GUI
General-Purpose Intermediate Containers
JPanel, JScrollPane , JSplitPane, JTabbedPane, JToolBar
Special-Purpose Intermediate Containers
JInternalFrame, JLayeredFrame, JRootPane
16
The Containment hierarchy cont’d
Atomic Components
Self-sufficient entities that present bits of information to
users. Often they also get input from the user.
Swing API provides many atomic components, including :
Basic controls
JButtons, JComboBox, JList, JMenu, JSlider, JTextField
Uneditable Information Displays
JLabel, JProgressBar, JToolTip
Editable Displays of Formatted Information
JColorChooser, JFileChooser, JTable, JTextArea, JTree
17
The Containment hierarchy cont’d
Atomic Components cont’d
JList: is the basic list class in swing.
18
Example for JList
import java.awt.FlowLayout; remove=new JButton("Remove");
import java.awt.event.*; frame.add(list);
import javax.swing.*; frame.add(remove);
public class ListExample { frame.setLayout(new FlowLayout());
JFrame frame; JList list; JButton remove; frame.setVisible(true);
DefaultListModel model; ButtonClick b=new ButtonClick();
ListExample(){ remove.addActionListener(b);
frame=new JFrame("List of IT students"); }
frame.setSize(300, 300); public static void main(String args[]){
model= new DefaultListModel(); ListExample e=new ListExample();
model.addElement("Alelign"); }
model.addElement("Bereket"); private class ButtonClick implements ActionListener{
model.addElement("Dejene"); public void actionPerformed(ActionEvent e)
model.addElement("Fikadu"); { model.removeElementAt(list.getSelectedIndex());
model.addElement("Habtamu"); }
list=new JList(model); }
list.setSelectedIndex(2); }
19
The Containment hierarchy cont’d
Atomic Components cont’d
JTable:
Used to display tables of data, optionally allowing the
user to edit the data.
JTable doesn't contain or cache data; it's simply a
view of your data.
20
Example for JTable
21
Example for JTable
Output
22
The Containment hierarchy cont’d
Atomic Components cont’d
JTree
Used to display hierarchical data.
A JTree object doesn't actually contain your data;
It simply provides a view of the data.
23
Example for JTree
24
Example for JTree
Output
25
Layout managers
The layout manager is a set of classes that implement the
java.AWT.LayoutManager interface and help to position the
components in a container.
The interface takes a task of laying out child components in
the container.
The advantages of this type of mechanism is that: when the
container is resized, layout manager automatically updates
the interface.
26
Layout managers cont’d
manager is used.
27
Layout managers cont’d
The basic layout managers include:
FlowLayout
GridLayout
BorderLayout
CardLayout
GridBagLayout
28
Layout managers cont’d
FlowLayout
It is the default Layout manager for the panel.
Lays out components line wise from left to right.
FlowLayout can be created using following constructors:
a. flowLaout(): Constructs a new layout with centered
alignment, leaving a vertical and horizontal gap.
29
Layout managers cont’d
FlowLayout cont’d
b. flowLaout(int align) : lets us to specify how each line is
aligned.
Valid values for align are as follows:
FlowLayout.LEFT
FlowLayout.CENTER
FlowLayout.RIGHT
FlowLayout.LEADING
FlowLayout.TRAILING
30
Layout managers cont’d
FlowLayout cont’d
c. flowLayout(int aling, int vgap, int hgap) :
Constructs a new flowlayout with the alignment
specified, leaving a vertical and horizontal gap as
specified.
Various methods can be used along with the flow layout.
For eg. getAlignment(), getHgap() etc.
31
Layout managers cont’d
Grid Layout
It lays out components in a way very similar to
spredsheet (rows and columns).
Specifying the number of rows and columns in grid
creates the Grid layout.
Grid Layout can be created using following constructors
a. gridLayout() : Creates a grid layout with a default of
one column per component in a single row.
32
Layout managers cont’d
Grid Layout cont’d
b. gridLayout(int row, int col) : creates a grid layout with
the specified number of rows and columns.
c. gridLayout(int rows, int cols, int hgap, int vgap) :
creates a grid layout with the specified rows and
columns and specified horizontal and vertical gaps.
33
Layout managers cont’d
BorderLayout
All areas need not be filled. The size of the areas will
34
Layout managers cont’d
BorderLayout cont’d
Can be created using following constructors:
a. borderLayout() : It creates a new border layout
with no gap between the components.
35
Layout managers cont’d
CardLayout
The CardLayout class is unique among the other
layout managers in that it stores several different
layouts.
Each layout can be thought of as being on a separate
index card in a deck that can be shuffled so that any
card is on top at a given time.
CardLayout provides these two constructors:
cardLayout( )
cardLayout(int horz, int vert)
36
Layout managers cont’d
GridBagLayout
Is a powerful layout manager. It is worth taking some
time to experiment with and explore.
Can specify the relative placement of components by
specifying their positions within cells inside a grid.
The key to the grid bag is that each component can
be a different size, and each row in the grid can
have a different number of columns.
This is why the layout is called a grid bag.
37
Thank you !!!
38