Unit 5 Awt - Swing
Unit 5 Awt - Swing
AWT contains large number of classes and methods that allows you
to create and manage graphical user interface ( GUI ) applications,
such as windows, buttons, scroll bars,etc. The AWT was designed to
provide a common set of tools for GUI design that could work on a
variety of platforms.
AWT is the foundation upon which Swing is made i.e Swing is a set
of GUI interfaces that extends the AWT. But now a days AWT is
merely used because most GUI Java programs are implemented using
Swing because of its rich implementation of GUI controls and light-
weighted nature.
Component class
Container
Panel
Panel class is a concrete subclass of Container. Panel does not
contain title bar, menu bar or border. It is container that is used for
holding components.
Window class
Window class creates a top level window. Window does not have
borders and menubar.
Frame
Swing
1. Platform Independent
2. Customizable
3. Extensible
4. Configurable
5. Lightweight
6. Rich Controls
7. Pluggable Look and Feel
The methods of Component class are widely used in java swing that
are given below.
Method Description
We can write the code of swing inside the main(), constructor or any
other method.
Frame( )
Frame(String title)
THE JAVA LIBRARY
Setting the Window’s Dimensions
Dimension getSize( )
This method returns the current size of the window contained
within the width and height fields of a Dimension object.
LayoutManagers
FlowLayout
Java BorderLayout
The BorderLayout 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 frame or window. The
BorderLayout provides five constants for each region:
GridLayout
Events
Event is an object that describes a state change in a source.
It can be generated as a consequence of a person interacting with the
elements in a graphical user interface.
Event Sources
A source is an object that generates an event.
Here, Type is the name of the event and el is a reference to the event
listener.
For example,
the method that registers a keyboard event listener is called
addKeyListener( ).
For example,
to remove a keyboard listener, you would call removeKeyListener( ).
Event Listeners
A listener is an object that is notified when an event occurs. It has two
major requirements.
First, it must have been registered with one or more sources to receive
notifications about specific types of events. Second, it must implement
methods to receive and process these notifications.
Event Classes:
int getID( )
ActionListener Interface
AdjustmentListener Interface
ComponentListener Interface
This interface defines four methods that are invoked when a component
is resized,
moved, shown, or hidden. Their general forms are shown here:
void componentResized(ComponentEvent ce)
void componentMoved(ComponentEvent ce)
void componentShown(ComponentEvent ce)
void componentHidden(ComponentEvent ce)
ContainerListener Interface
WindowListener:
void windowActivated(WindowEvent we)
void windowClosed(WindowEvent we)
void windowClosing(WindowEvent we)
void windowDeactivated(WindowEvent we)
void windowDeiconified(WindowEvent we)
void windowIconified(WindowEvent we)
void windowOpened(WindowEvent we)
JButton
The JButton class is used to create a labeled button that has platform
independent implementation. The application result in some action
when the button is pushed. It inherits AbstractButton class.
Constructor Description
JButton() It creates a button with no text and icon.
JButton(String
It creates a button with the specified text.
s)
It creates a button with the specified icon
JButton(Icon i)
object.
Methods Description
It is used to set specified text
void setText(String s)
on button
It is used to return the text of
String getText()
the button.
It is used to enable or disable
void setEnabled(boolean b)
the button.
It is used to set the specified
void setIcon(Icon b)
Icon on the button.
It is used to get the Icon of the
Icon getIcon()
button.
It is used to set the mnemonic
void setMnemonic(int a)
on the button.
void It is used to add the action
addActionListener(ActionListener a) listener to this object.
JTextField:
Constructor Description
JTextField() Creates a new TextField
Creates a new TextField initialized with the
JTextField(String text)
specified text.
JTextField(String text, int Creates a new TextField initialized with the
columns) specified text and columns.
Creates a new empty TextField with the
JTextField(int columns)
specified number of columns.
JTextArea
Constructor Description
Creates a text area that displays no text
JTextArea()
initially.
Creates a text area that displays specified text
JTextArea(String s)
initially.
Creates a text area with the specified number
JTextArea(int row, int
of rows and columns that displays no text
column)
initially.
JTextArea(String s, int Creates a text area with the specified number
row, int column) of rows and columns that displays specified
text.
Commonly used Methods:
Methods Description
void setRows(int rows) It is used to set specified number of rows.
void setColumns(int cols) It is used to set specified number of columns.
void setFont(Font f) It is used to set the specified font.
Java JPasswordField
Constructor Description
Constructs a new JPasswordField, with a
JPasswordField() default document, null starting text string,
and 0 column width.
JPasswordField(int Constructs a new empty JPasswordField
columns) with the specified number of columns.
Constructs a new JPasswordField initialized
JPasswordField(String text)
with the specified text.
JPasswordField(String text, Construct a new JPasswordField initialized
int columns) with the specified text and columns.
Java JComboBox
1.
Constructor Description
Methods Description