Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views29 pages

Unit - 5

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 29

UNIT-5

GUI Programming with Swing - The AWT class hierarchy,


Introduction to Swing, Swing Vs AWT, Hierarchy for Swing
components, Overview of some Swing components – Jbutton,
JLabel, JTextField, JTextArea, simple Swing applications,
Layout management – Layout manager types – border, grid
and flow
Event Handling- Events, Event sources, Event classes, Event
Listeners, Delegation event model, Examples: Handling
Mouse and Key events, Adapter classes.

Dr Jayapal
mrcetjp@gmail.com
ABSTRACT WINDOW TOOLKIT (AWT)
Java AWT (Abstract Window Toolkit) is an API to develop GUI
or window-based application in java. Java AWT components
are platform-dependent i.e. components are displayed
according to the view of operating system. AWT is heavyweight
i.e. its components uses the resources of system. The Abstract
Window Toolkit(AWT) support for applets. The AWT contains
numerous classes and methods that allow you to create and
manage windows.
The java.awt package provides classes for AWT api such as
TextField, Label, TextArea, RadioButton, CheckBox, Choice,
List etc.

Dr Jayapal
mrcetjp@gmail.com
Java AWT Hierarchy

Dr Jayapal
mrcetjp@gmail.com
AWT Classes
The AWT classes are contained in the java.awt package. It is one of
Java's largest packages.
Class Description
AWTEvent Encapsulates AWT events.
AWTEventMulticaster Dispatches events to multiple
listeners.
BorderLayout Border layouts use five
components: North, South, East,
West, and Center.
CardLayout Card layouts emulate index cards.
Only the one on top is showing.
Checkbox Creates a check box control.
CheckboxGroup Creates a group of check box
controls.
CheckboxMenuItem Creates an on/off menu item.
Choice Creates a pop-up list.
Color Manages colors in a portable,
Drplatform-independent
Jayapal fashion.
mrcetjp@gmail.com
Class Description
Component An abstract superclass for various AWT
components.
Container A subclass of Component that can hold
other components.
Cursor Encapsulates a bitmapped cursor.
Dialog Creates a dialog window.
Dimension Specifies the dimensions of an object.
The width is stored in
width , and the height is stored in height
Event Encapsulates events.
FlowLayout The flow layout manager. Flow layout
positions components left to right, top
to bottom.
Frame Creates a standard window that has a
title bar, resize corners, and a menu bar.
Graphics Encapsulates
Dr Jayapal the graphics context.
mrcetjp@gmail.com
Components:All the elements like the button, text fields, scroll bars, etc. are
called components. In order to place every component in a particular position
on a screen, we need to add them to a container.

Container:The Container is a component in AWT that can contain another


components like buttons, textfields, labels etc. The classes that extends
Container class are known as container such as Frame, Dialog and Panel.
Types of containers:
1. Window
2. Panel
3. Frame
4. Dialog

Dr Jayapal
mrcetjp@gmail.com
Window: window is the container that have no borders and menu bars.
frame, dialog or another window is used for creating a window. Instance of
Window class is used to create this container.

Panel:Panel is the container that doesn't contain title bar, border or menu
bar. It is generic container for holding the components. It can have other
components like button, text field etc. An instance of Panel class creates a
container, in which components can be added.

Frame:Frame is the container that contain title bar and border and can have
menu bars. It can have other components like button, text field, scrollbar etc.
Frame is most widely used container while developing an AWT application.

Dialog:Dialog boxes are graphical components that are usually used to display
errors or give some other information to the user.

Dr Jayapal
mrcetjp@gmail.com
There are two ways to create a GUI using Frame in AWT.
1. By extending Frame class (inheritance)
2. By creating the object of Frame class (association)

Dr Jayapal
mrcetjp@gmail.com
There are two ways to create a GUI using Frame in AWT.
1. By extending Frame class (inheritance)
2. By creating the object of Frame class (association)

Control Fundamentals
The AWT supports the following types of controls:
1. Labels
2. Push buttons
3. Check boxes
4. Choice lists
5. Lists
6. Scroll bars
7. Text editing

Dr Jayapal
mrcetjp@gmail.com
SWINGS
Swing is a set of classes that provides more powerful and flexible
components than are possible with the AWT. Swing is a GUI
widget toolkit for Java. It is part of Oracle's Java Foundation
Classes (JFC) that is used to create window-based applications. It
is built on the top of AWT (Abstract Windowing Toolkit) API and
entirely written in java.
The javax.swing package provides classes for java swing
API such as JButton, JTextField, JTextArea, JRadioButton,
JCheckbox, JMenu, JColorChooser etc.

Dr Jayapal
mrcetjp@gmail.com
Differences between AWT and Swing

AWT Swing

AWT components are called Swings are called light weight


Heavyweightcomponent. component because swing components
sits on the top of AWT components and
do the work.
AWT components are platform Swing components are made in purely
dependent. java and they are platform independent.
AWT components require java.awt Swingcomponents require
package. javax.swing package.
AWT is a thin layer of code on top of Swing is much larger. Swing also has
the OS. verymuch richer functionality.
AWT stands for Abstract windows Swing is also called as JFC’s (Java
toolkit. Foundationclasses).

Dr Jayapal
mrcetjp@gmail.com
Differences between AWT and Swing
AWT Swing

This feature is not supported in We can have different look and feel in Swing.
AWT.
Using AWT, you have to
implement a lot ofthings Swing has them built in.
yourself.

Swing has many advanced features like JTabel,


Jtabbed pane which is not available in AWT.
This feature is not available in Also. Swing components are called
AWT. "lightweight" because they do not require a
native OS object to implement their
functionality. JDialog and JFrame are
heavyweight, because they do have a peer. So
components like JButton, JTextArea, etc., are
lightweight because they do not have an OS
peer.
Dr Jayapal
mrcetjp@gmail.com
The Swing component classes are:
Class Description
AbstractButton Abstract superclass for Swing buttons.
ButtonGroup Encapsulates a mutually exclusive set of buttons.
ImageIcon Encapsulates an icon.
JApplet The Swing version of Applet.
JButton The Swing push button class.
JCheckBox The Swing check box class.
JComboBox Encapsulates a combo box (combination of a drop-down list
& text field).
JLabel The Swing version of a label.
JRadioButton The Swing version of a radio button.
JScrollPane Encapsulates a scrollable window.
JTabbedPane Encapsulates a tabbed window.
JTable Encapsulates a table-based control.
JTextField The Swing version of a text field.
JTree Encapsulates a tree-based control.
Dr Jayapal
mrcetjp@gmail.com
Hierarchy for Swing components

Dr Jayapal
mrcetjp@gmail.com
JApplet
Fundamental to Swing is the JApplet class, which extends Applet.
Applets that use Swing must be subclasses of JApplet. JApplet is
rich with functionality that is not foundin Applet.
The content pane can be obtained via the method shown here:
Container getContentPane( )
The add( ) method of Container can be used to add a component
to a content pane. Its form is shown here:
void add(comp)
Here, comp is the component to be added to the content pane.

Dr Jayapal
mrcetjp@gmail.com
JFrame
Create an object to JFrame: JFrame ob = new JFrame ("title"); (or)
Create a class as subclass to JFrame class: MyFrame extends
JFrame Create an object to that class : MyFrame ob = new
MyFrame ();

To close the frame,use getDefaultCloseOperation () method of


JFrame class,
as shown here: getDefaultCloseOperation (constant);
where the constant can be any one of the following:
JFrame.EXIT_ON_CLOSE: This closes the application upon clicking
on close button.
JFrame.DISPOSE_ON_CLOSE: This disposes the present frame
which is visible on the screen. The JVM may also terminate.
JFrame.DO_NOTHING_ON_CLOSE: This will not perform any
operation upon clicking on close button.
JFrame.HIDE_ON_CLOSE: This Drhides Jayapal the frame upon clicking on

close button. mrcetjp@gmail.com


Window Panes: In swings the components are attached to the
window panes only. A window pane represents a free area of a
window where some text or components can be displayed. For
example, we can create a frame using JFrame class in javax.swing
which contains a free area inside it, this free area is called
'window pane'. Four types of window panes are available in
javax.swing package.

Glass Pane: This is the first pane and is very close to the
monitors screen. Any components to be displayed in the
foreground are attached to this glass pane. To reach this glass
pane, we use getGlassPane () method of JFrame class.

Dr Jayapal
mrcetjp@gmail.com
Root Pane: This pane is below the glass pane. Any components
to be displayed in the background are displayed in this pane.
Root pane and glass pane are used in animations also.
For example, suppose we want to display a flying aeroplane in
the sky. The aeroplane can be displayed as a .gif or .jpg file in the
glass pane where as the blue sky can be displayed in the root
pane in the background. To reach this root pane, we use
getRootPane () method of JFrame class.
Layered Pane: This pane lies below the root pane. When we
want to take several components as a group, we attach them in
the layered pane. We can reach this pane by calling
getLayeredPane () method of JFrame class.

Content Pane: This is the bottom most pane of all. Individual


components are attached to this pane. To reach this pane, we
can call getContentPane () method of JFrame class.
Dr Jayapal
mrcetjp@gmail.com
Displaying Text in the Frame:
paintComponent (Graphics g) method of JPanel class is used to
paint the portion of a component in swing. We should override
this method in our class. In the following example, we are writing
our class MyPanel as a subclass to JPanel and override the
painComponent () method.

Write a program to display text in the frame


Dr Jayapal
mrcetjp@gmail.com
TEXT FIELDS:
The Swing text field is encapsulated by the JTextComponent class,
which extends JComponent.
It provides functionality that is common to Swing text
components.
One of its subclasses is JTextField, which allows you to edit one
line of text.
Some of its constructors are shown here:
1. JTextField( )
2. JTextField(int cols)
3. JTextField(String s, int cols)
4. JTextField(String s)

Write a program to display text in the frame


Dr Jayapal
mrcetjp@gmail.com
JButton
JLabel
JTextField
JTextArea
JCheckBox
JRadioButton
JList
JScrollBar
JDialog
JPanel
JFrame
JTextPane

And many more... Dr Jayapal


mrcetjp@gmail.com
Layout Managers
The Layout Managers are used to arrange components in a
particular manner. The Java Layout Managers facilitates us to
control the positioning and size of the components in GUI forms.
Layout Manager is an interface that is implemented by all the
classes of layout managers

Dr Jayapal
mrcetjp@gmail.com
Border Layout
The Border Layout 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 a
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

Constructors of BorderLayout class:


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 verticalmrcetjp@gmail.com
gaps between the components.
Dr Jayapal
GridLayout
The Java GridLayout class is used to arrange the components in a rectangular
grid. One component is displayed in each rectangle.

Constructors of GridLayout class


GridLayout(): creates a grid layout with one column per
component in a row.
GridLayout(int rows, int columns): creates a grid layout with the
given rows and columns but no gaps between the components.
GridLayout(int rows, int columns, int hgap, int vgap): creates a
grid layout with the given rows and columns along with given
horizontal and vertical gaps.

Dr Jayapal
mrcetjp@gmail.com
FlowLayout
The Java FlowLayout class is used to arrange the
components in a line, one after another (in a flow). It is
the default layout of the applet or panel.

Fields of FlowLayout class


public static final int LEFT
public static final int RIGHT
public static final int CENTER
public static final int LEADING
public static final int TRAILING

Dr Jayapal
mrcetjp@gmail.com
FlowLayout
Constructors of FlowLayout class
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, int vgap): creates a flow
layout with the given alignment and the given horizontal
and vertical gap.

Dr Jayapal
mrcetjp@gmail.com
Java Event classes and Listener interfaces
Event Classes Listener Interfaces
ActionEvent ActionListener
MouseEvent MouseListener and MouseMotionListener

MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
Dr Jayapal
mrcetjp@gmail.com
Event Handling can be performed by Registering methods.

For registering the component with the Listener, many classes provide the
registration methods. For example:
1.Button
public void addActionListener(ActionListener a){}
2.MenuItem
public void addActionListener(ActionListener a){}
3.TextField
public void addActionListener(ActionListener a){}
public void addTextListener(TextListener a){}
4.TextArea
public void addTextListener(TextListener a){}
5.Checkbox
public void addItemListener(ItemListener a){}
6.Choice
public void addItemListener(ItemListener a){}
7.List
public void addActionListener(ActionListener a){}
public void addItemListener(ItemListener a){}

Dr Jayapal
mrcetjp@gmail.com
Applets
Applet is a special type of program that is embedded in the webpage to
generate the dynamic content. It runs inside the browser and works at client
side.
Advantage of Applet
•There are many advantages of applet. They are as follows:
It works at client side so less response time.
•It
Secured
•Secured
•It can be executed by browsers running under many plateforms, including
Linux, Windows, Mac Os etc.
Drawback of Applet
•Plugin is required at client browser to execute applet.

Dr Jayapal
mrcetjp@gmail.com

You might also like