CH 2-Abstract Window Toolkit
CH 2-Abstract Window Toolkit
ABSTRACT WINDOW
TOOLKIT
Introduction
The java.awt package provides classes for AWT API such as TextField, Label,
TextArea, RadioButton, CheckBox, Choice, List etc.
AWT Class Hierarchy
AWT Class Hierarchy
AWT Class Hierarchy
Component
A graphical user interface is built using different graphical elements called
components.
In AWT we have classes for each component as shown in the above diagram.
AWT Class Hierarchy
Container
Subclass of component
The classes that extends Container class are known as container such as Frame,
Dialog and Panel.
Window
The window is the container that have no borders and menu bars. You must use frame,
dialog or another window for creating a window.
Panel
The Panel is the container that doesn't contain title bar and menu bars.
Frame
A Frame is a top-level window with a title and a border.
The size of the frame includes any area designated for the border.
It can contain several components like buttons, text fields, scrollbars etc.
canvas
Generally, it’s not part of a any hierarchy. But its another valuable type of
window.
Next, override any of the standard applet methods, such as init( ), start( ), and stop(
), and paint to show or hide the frame as needed.
By this the frame window only comes into existence, but it will not visible. It will be
visible by calling setVisible() method.
After creating it has default size, which can be changed by using setSize().
Frame windows
Frame’s constructors:
Frame( )
Frame(String title)
Note:
first form will create standard window without having any title.
Second will create window with the specified title.
Frame windows
We must set the size of the window after it has been created.
Some of the methods used when working with windows are as follows:
Frame windows
The new size of the window is specified by newWidth and newHeight, or by the
width and height fields of the Dimension object passed in newSize. The
dimensions are specified in terms of pixels.
Frame windows
Dimension getSize( )
This method returns the current size of the window contained within the width
and height fields of a Dimension object.
Frame windows
After a frame window has been created, it will not be visible until you call
setVisible().
When using a frame window, your program must remove that window
from the screen when it is closed, by calling setVisible(false).
import java.awt.Frame;
public class CreateFrameWindowExample extends Frame
{
CreateFrameWindowExample(String title)
{
super();
this.setTitle(title);
this.setVisible(true);
}
Creating Frame windows in Applet
GUI elements : These are the core visual elements the user eventually
sees and interacts with. AWT provides a huge list of widely used and
common elements varying from basic to complex.
Layouts: They define how GUI elements should be organized on the screen
and provide a final look and feel to the GUI (Graphical User Interface).
Behavior: These are events which occur when the user interacts with GUI
elements.
AWT Controls
Component
A Component is an abstract super
class for GUI controls and it represents
an object with graphical
representation.
AWT UI Elements
Choice : A Choice control is used to show pop up menu of choices. Selected choice is
shown on the top of the menu.
Canvas : A Canvas control represents a rectangular area where application can draw
something or can receive inputs created by user.
Image : An Image control is superclass for all image classes representing graphical
images.
Scroll Bar : A Scrollbar control represents a scroll bar component in order to enable
user to select from range of values.
Dialog : A Dialog control represents a top-level window with a title and a border used
to take some form of input from the user.
File Dialog : A File Dialog control represents a dialog window from which the user can
select a file.
Useful Methods of Component class
Method Description
public void add(Component c) inserts a component on this component.
public void setSize(int width,int height) sets the size (width and height) of the
component.
Label is a passive control because it does not create any event when accessed
by the user.
The label control is an object of Label.
A label displays a single line of read-only text.
However the text can be changed by the application programmer but cannot be
changed by the end user in any way.
Class declaration
Following is the declaration for java.awt.Label class:
public class Label
extends Component
implements Accessible
Continue….
Constructor Description
Label() Constructs an empty label.
Label(String text) Constructs a new label with the specified string of text,
left justified.
Label(String text, int Constructs a new label that presents the specified string of
alignment) text with the specified alignment.
Continue…
Method Description
/*
* To create a label with it's text alignment use
* Label(String text, int alignment)
* constructor of AWT Label class.
*
* The possible alignment values are
* Label.LEFT, Label.RIGHT, and Label.CENTER
*/
Label label1 = new Label("Left Aligned Label", Label.LEFT);
//add labels
add(label1);
add(label2);
add(label3);
/*
* To change alignment of label's text use
* void setAlignment(int alignment)
* method of AWT Label class.
*/
}
}
TextField
import java.awt.*;
class TextFieldExample
{
public static void main(String args[]){
Frame f= new Frame("TextField Example");
TextField t1,t2;
t1=new TextField("Welcome to Java");
t1.setBounds(50,100, 200,30);
TextField Example
Constructors
Button()
Button( String text)
Method Description
String getLabel() Get the label on the button
void setLabel(String text) Sets the label of button
Example
import java.applet.Applet;
import java.awt.*;
Layout manager tells java where to put components when they are added to
container.
Layout Managers
GridLayout
BorderLayout
CardLayout
Flow Layout
When you add components to the screen, they will flow left to right
(centered) based on the order added and the width of the screen.
Constructors
FlowLayout()
FlowLayout(int alignType)
FlowLayout(int alignType, int hGap, int vGap)
import java.awt.*;
These are named after the four different borders of the screen, North,
When a Component is added to the layout, you must specify which area to
place it in.
Constructors
import java.awt.*;
setLayout(new BorderLayout());
GridLayout(): creates a default grid layout with default row and column.
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.
Example
Each component in a container with this layout fills the entire container.
The name card layout evolves from a stack of cards where one card is piled
upon another and only one of them is shown.
In this layout, the first component that you add to the container will be at
the top of stack and therefore visible and the last one will be at the bottom.
Continue…
Constructors of CardLayout
Constructor Description
Cardlayout() Creates a card layout with zero horizontal
and vertical gap.
CardLayout(int hgap, int Creates a card layout with the given
vgap) horizontal and vertical gap.
Summary of Layout Managers
Graphical User Interface with Swing
It is part of Java Foundation Classes (JFC) that is used to create window-based applications.
The Swing components are 100% pure java window implementation to support them.
Swing provides programmer the facility to change the look and feel of components being displayed on
any system. This is called ‘plaf’(pluggable look and feel)
Feel represents how the user can interact with the component.
The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea,
JRadioButton, JCheckbox, JMenu, JColorChooser etc.
Difference between AWT and Swing
Hierarchy of Java Swing Classes
Methods of Component Class
Method Description
public void setLayout(LayoutManager m) sets the layout manager for the component.
public void setVisible(boolean b) sets the visibility of the component. It is by default false.
Continue…
JFrame, JPanel, JWindow is the Swing's version of Frame, Panel and Windows
respectively.
JApplet
The JApplet class extends the Applet class.
We can use JApplet that can have all the controls of swing.
JLabel
A JLabel is a single line label similar to java.awt.Label. Constructor:
Constructor Description
Jlabel() Creates label with no image and with an empty string for the title.
JLabel(Icon image) Creates a label with the specified image.
JLabel(String text) Creates a label with the specified text.
Continue…
JTextField : The class JTextField is a component which allow the editing of a single
line of text.
Jbutton : A JButton can be used in a GUI just like a java.awt.Button. It behaves like
an AWT Button, notifying ActionListener list elements when pushed.
JCheckBox : A JCheckBox is similar to an AWT Checkbox that is not in a
CheckboxGroup.
Purpose of the check box - an item that can be selected or deselected, and which displays its
state to the user.
JRadioButton : The JRadioButton class is used to create a radio button.
It is used to choose one option from multiple options.
It should be added in Group to select one radio button only by using ButtonGroup class.
By default radio button is not selected.
Continue…
Menus : Every top-level window has a menu bar associated with it.
This menu bar consists of various menu choices available to the end user.
Further each choice contains list of options which is called drop down menus.
We can write the code of swing inside the main(), constructor or any
other method.
EXAMPLE
import javax.swing.*;
public class SwingExample {
Listeners : A listener is an object that listens the event. A listener gets notified when an event occurs.
Continue…
A source generates an Event and sends it to one or more listeners registered with the
source.
Once event is received by the listener, they process the event and then return.
Events are supported by a number of Java packages, like java.util, java.awt and
java.awt.event.
Reacts to
the event
EVENT SOURCE EVENT LISTENER
Fires an event
object
Source
registers
listener
Continue…
It is the visual component with which user has interacted. Event source creates
the event object when event has occurred.
For example if we want just one method to implement still we need to define
other methods also due to rule of interface implementation.
For that java API provides several Adapter classes for each Listener interface.
Listener Adapter
ComponentListener ComponentAdapter
ContainerListener ContainerAdapter
FocusListener FocusAdapter
KeyListener KeyAdapter
MouseListener MouseAdapter