Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 38

Objectives

 After completing this presentation you will be able to:


 Identify powerful GUI components that are found in
swing but not in AWT.
 Classify swing components as Top-level, intermediate-
level and atomic components.
 Use layout managers to position swing components.
 Identify the differences between AWT and Swing.

1
Introduction

 Just as the connection points between program


components are called interfaces, the “connection point”
between a program and its human user is called its “user
interface.”
 A program that uses visual aids(buttons, scroll bars,
menus,
etc.)to help a user enter input and read output is called a
Graphical User Interface (GUI)

2
Introduction cont’d

 In Java, the framework for building GUIs and connecting them


to controllers and models is called AWT/Swing.
 The AWT part is the java.awt package.
 The Swing part is the javax.swing package.
 “AWT” stands for “Abstract Window Toolkit.”

3
Event handling

 When an event like a button push or a mouse movement


occurs, it occurs within an object within the window.
 The object in which the event occurs is the event source.
 In AWT/Swing, when an event occurs, the event source
automatically sends a message to an object, called its
event listener, to handle the event.

4
Swing and AWT

 Swing is a framework that provides more powerful and


flexible GUI components than AWT does.
 Swing did not exist in the early days of Java. Rather, it was
a response to deficiencies present in Java’s original GUI
subsystem AWT
 The AWT defines a basic set of controls, windows, and
dialog boxes that support a usable, but limited graphical
interface.

5
Swing and AWT cont’d

 One reason for the limited nature of the AWT is that it


translates its various visual Components into their
corresponding platform-specific equivalents, or peers.
 This means that the look and feel of a component is
defined by the platform, not by Java.
 Because the AWT components use native code resources,
they are referred to as heavyweight.

6
Swing and AWT cont’d

 The use of native peers led to several problems.


 First, because of variations between operating systems,
a component might look, or even act, differently on
different platforms.
 This potential variability threatened the overarching
philosophy of Java: write once, run anywhere.
 Second, the look and feel of each component was fixed
(because it is defined by the platform) and could not be
(easily) changed
7
Swing and AWT cont’d
 …cont’d.
 Third, the use of heavyweight components caused
some frustrating restrictions.
 For example, a heavyweight component was always
opaque.
 Swing was introduced in 1997 to overcome the limitations
of AWT.
 Initially available with Java 1.1 as a separate library.
 Beginning with Java 1.2, fully integrated into Java.

8
Swing and AWT cont’d
 Although Swing eliminates a number of limitations
inherent in the AWT, Swing does not replace it.

 Instead, Swing is built on the foundation of the AWT. This is


why the AWT is still a crucial part of Java.

 Swing also uses the same event handling mechanism as


the AWT. Swing components do not contain peers.

 Swing components allow mixing AWT heavyweight and


swing lightweight components in an application.

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:

 JApplet: Enables applets to use Swing components.

 JDialog: The main class for creating a dialog window.

 JFrame: A top-level window with a title and a border.

 JWindow: Provides a window with no controls or title.

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.

 It supports the selection of one or more items from a list.

 Although the list often consists of strings, it is possible to

create a list of just about any object that can be displayed.

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

 The layout manager is set by the setLayout( ) method.

 If no call to setLayout( ) is made, then the default layout

manager is used.

The setLayout( ) method has the following general form:

 void setLayout(LayoutManager layoutObj)

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

 In this layout the components are placed on the border

of the container based on the following options:

 CENTER, SOUTH, EAST, WEST, NORTH

 All areas need not be filled. The size of the areas will

depend on the components they contain.

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.

b. borderLayout(int hgap, int vgap) : It creates a


border layout with the specified horizontal and vertical
gap between 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

You might also like