You are on page 1of 26

JAVA Software Development

Paradigm
Advance OO Programming
BSCS Semester 6
MCS 3
Course Instructor: Ms. Iram

Window Fundamental
Component
Container
Window

Frame

Panel
Applet
7-2

Abstract Windowing Toolkit (AWT)


Swings

7-3

Swing vs AWT
AWT is Javas original set of classes for building GUIs

Uses peer components of the OS; heavyweight


Not truly portable
Due to OSs underlying display management system

Swing is designed to solve AWTs problems

99% java; lightweight components


Drawing of components is done in java

Lays out consistently on all OSs


Uses AWT event handling

Swings
PLAF (Pluggable Look and Feel).
Java provides several LAFs, including:

Metal platform independent


Windows for Windows
Windows Classic like Windows 95
Motif for Unix / Linux

try
{
UIManager.setLookAndFeel(plafName);
}
catch (Exception ex) { ... }
16-5

GUI Components
Components are created using constructors:
JLabel guest = new JLabel ("Guest);

To be usable, a component must be added to


the applications content pane or to another
component:
JPanel scorePanel = new JPanel();
scorePanel.add (guest);

16-6

Implementing a Swing GUI


Import javax.swing.*, java.io.*, java.awt.*
Make a specific class to do GUI functions
Specify all the GUI functions/components in the
classs constructor (or methods / classes called by
the constructor)

Run the GUI by instantiating the class in the classs


main method

Implementing a Swing GUI

JFrame
Frames are the basis of any Java GUI
Frame is the actual window that encompasses your

GUI objects; a GUI can have multiple frames


The J prefix is at the beginning of any Swing
components name (to distinguish them from AWT
components)
JFrame is a wrapper around AWTs Frame

Frame/Pane

Panel/JPanels
If a frame is a window, a panel is the glass
Panes hold a windows GUI components
Every frame has at least one pane, the default

Content Pane
Useful for layout
Needed to add components to the frame
When a frame is created, the content pane is created
with it
JScrollPane is useful for embedding tables and text
areas

JScrollPane newPane = new JScrollPane(JTextArea area);

JScrollPane

Swing components
Components discussed:

JLabel
JButton
JToggleButton
JCheckBox
JComboBox

Chapter:

16

JSlider
JTextField
JPasswordField
JTextArea
16-13

Model-View-Controller
Design pattern often used in Swing objects
Breaks a GUI object down into three parts

Model manages the data used by the object


View manages the graphical/textual output of the
object
Controller interprets user input, commanding the
model and view to change as necessary

Model-View-Controller
Swing components that use the MVC pattern,
such as JList and JTable, generally have one
class that controls both the view and the
controller and a separate class that controls
the model

Model-View-Controller
Programmer instantiates a model (e.g., the

DefaultTableModel class), then loads that model with the


data to be displayed in the GUI

JTable table = new JTable(DefaultTableModel model);

If the programmer instantiates the GUI object without a

model, the view/controller class creates an empty model to


work from

Layout Managers
Every panel has a layout manager
Layout managers tell Java where to put components when
you add them to a pane

awt/Swing support several layout managers. These


classes implement the java.awt.LayoutManager interface

FlowLayout : left to right order


GridLayout : matrix dimensions
BorderLayout: five regions: North, South, East, West, and Center
BoxLayout : absolute positioning

The default layout manager is FlowLayout.


A layout is chosen by calling the containers setLayout
method.

Default Layouts
Each component has a default layout

manager, which remains in effect until the


components setLayout method is called.
The defaults are:
Content pane
JPanel
Box

BorderLayout
FlowLayout
BoxLayout

16-18

FlowLayout
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add (new JButton ("Back to Start"));
c.add (new JButton ("Previous Slide"));
c.add (new JButton ("Next Slide"));
c.add (new JButton ("Last Slide"));
c.add (new JButton ("Exit"));

16-19

BoxLayout
In a horizontal box, components are placed
horizontally, left to right.

In a vertical box, components are placed


vertically, top to bottom.
Horizontal or
vertical has
nothing to do with
the shape of the
box itself.
16-20

BoxLayout (contd)
BoxLayout is the default layout for a Box
container.

Box box1 = Box.createHorizontalBox();


box1. add (...);
// add a spacer, 60 pixels:
box1.add(Box.createHorizontalStrut (60));
Box box2 = Box.createVerticalBox();
...
16-21

BoxLayout (contd)
Container c = getContentPane();
c.setLayout(new FlowLayout());
Box box = Box.createVerticalBox();
box.add (new JButton ("Next Slide"));
box.add (new JButton ("Previous Slide"));
box.add (Box.createVerticalStrut (20) );
box.add (new JButton ("Exit"));
c.add (box);

Adds extra
vertical space
between
components

16-22

GridLayout
Splits the panel into a grid with given

numbers of rows and columns.


Places components into the grid cells.
Forces the size of each component to occupy
the whole cell.
Allows additional spacing between cells.

16-23

GridLayout (contd)
Container c = getContentPane();
c.setLayout (new GridLayout(3, 2, 10, 20 ));
c.add (new JButton ("Back to Start"));
c.add (new JButton ("Previous Slide"));
c.add (new JButton ("Next Slide"));
Extra space
c.add (new JButton ("Last Slide"));
between the
c.add (new JButton ("Exit"));
cells (in pixels)

16-24

BorderLayout
Divides the area into five regions and adds a
component to the specified region.
NORTH
WEST

CENTER

EAST

SOUTH

Forces the size of each component to occupy


the whole region.

16-25

BorderLayout (contd)
Container c = getContentPane();
c.setLayout(new BorderLayout()); // optional: default
c.add (new JButton ("Next Slide"), BorderLayout.EAST);
c.add (new JButton ("Previous Slide"), BorderLayout.WEST);
c.add (new JButton ("Back to Start"), BorderLayout.NORTH);
c.add (new JButton ("Last Slide"), BorderLayout.SOUTH);
c.add (new JButton ("Exit"), BorderLayout.CENTER);

16-26

You might also like