Unit-I Abstract Window Toolkit (Autosaved)
Unit-I Abstract Window Toolkit (Autosaved)
Unit Outcomes
Develop GUI programs using AWT Components for the
given problem
given problem
Sub-Topics
Component, container, window, frame, panel.
AWT controls
Graphical User Interface (GUI) offers user interaction via some graphical components
such as window,frame,Panel, Button, Textfield, TextArea, Listbox, Combobox, Label,
Checkbox etc.
Using these components we can create an interactive user interface for an application.
GUI provides result to end user in response to raised events.GUI is entirely based events.
For example clicking over a button, closing a window, opening a window, typing
something in a textarea etc. These activities are known as events.
AWT Classes
The AWT classes are contained in the java.awt package
It is one of Java’s largest packages.
AWT Class Hierachy
Windows Fundamental
The most common windows are derived from :
Panel : Used by Applet
Frame : Creates standard Window
Component
• It defines over a hundred public methods that are responsible for managing
events, such as mouse and keyboard input, positioning and sizing the
window, and repainting.
public void setSize(int width, int height) sets the size (width and height) of
the component.
14
Container
• The Container is a component in AWT that can contain another
• void setFont(Font f)
Constructor are:
Frame()
Frame(String title)
Push Buttons
Check boxes
Choice lists
Lists
Scroll bars
Adding and Removing Controls
After creating control that needs to be added to window by calling add()
Syntax:
class.
Syntax:
displays.
Label( )
Label(String str)
The second version creates a label that contains the string specified by str. This string is
left-justified.
The third version creates a label that contains the string specified by str using the
alignment specified by how. The value of how must be one of these three constants:
Label.LEFT, Label.RIGHT, or Label.CENTER.
Label : Methods
String getText( )
int getAlignment( )
Label : Program
// Demonstrate Labels
public void init()
import java.awt.*; {
import java.applet.*; Label one = new Label("One");
/* Label two = new Label("Two");
<applet code="LabelDemo" Label three = new Label("Three");
width=300 height=200> // add labels to applet window
</applet> add(one);
*/ add(two);
add(three);
public class LabelDemo extends
Applet }
}
{
Buttons
Button( )
Button(String str)
Buttons : Methods
String getLabel()
void addActionListener(ActionListener l)
void removeActionListener(ActionListener l)
String getActionCommand()
Checkbox( )
Checkbox(String str)
boolean getState( )
void setState(boolean on)
String getLabel( )
void setLabel(String str)
void addItemListener(ItemListener l)
void removeItemListener(ItemListener l)
void setCheckboxGroup(CheckboxGroup g)
CheckboxGroup getCheckboxGroup()
Checkbox : Program
public void init()
// Demonstrate check boxes. {
import java.awt.*; Win98 = new Checkbox("Windows 98/XP",
import java.applet.*; null, true);
winNT = new Checkbox("Windows
/*
NT/2000");
<applet code="CheckboxDemo" solaris = new Checkbox("Solaris");
width=250 height=200> mac = new Checkbox("MacOS");
</applet> add(Win98);
*/ add(winNT);
public class CheckboxDemo extends add(solaris);
add(mac);
Applet
}
{ public void paint(Graphics g)
String msg = ""; {}
Checkbox Win98, winNT, solaris, mac; }
CheckboxGroup
It is possible to create a set of mutually exclusive check boxes in
which one and only one check box in the group can be checked
at any one time.
group.
CheckboxGroup : Methods
Checkbox getSelectedCheckbox( )
Each item in the list is a string that appears as a left justified label in the
Choice only defines the default constructor, which creates an empty list
Methods
String getSelectedItem( )
int getSelectedIndex( )
int getItemCount( )
selection list.
Unlike the Choice object, which shows only the single selected
List( )
List(int numRows)
List(int numRows, boolean multipleSelect)
The first version creates a List control that allows only one item to be
selected at any one time.
In the second form, the value of numRows specifies the number of
entries in the list that will always be visible (others can be scrolled into
view as needed).
In the third form, if multipleSelect is true, then the user may select two
or more items at a time.
List : Methods
void add(String name)
String getSelectedItem( )
int getSelectedIndex( )
String[ ] getSelectedItems( )
int[ ] getSelectedIndexes( )
int getItemCount( )
The slider box can be dragged by the user to a new position, this
Scrollbar(int style)
Scrollbar(int style, int iValue, int tSize, int min, int max)
int getValue( )
int getMinimum( )
int getMaximum( )
Text fields allow the user to enter strings and to edit the text
using the arrow keys, cut and paste keys, and mouse
selections.
TextField(int numChars)
TextField(String str)
Fourth form initializes textfield with string contained in str and sets
its width.
TextField : Methods
String getText( )
String getSelectedText( )
boolean isEditable( )
boolean echoCharIsSet( )
char getEchoChar( )
TextArea
Sometimes a single line of text input is not enough for a given task.
To handle these situations, the AWT includes a simple multiline editor called TextArea.
TextArea( )
TextArea(int numLines, int numChars)
TextArea(String str)
TextArea(String str, int numLines, int numChars)
TextArea(String str, int numLines, int numChars, int sBars)
Methods
TextArea is a subclass of TextComponent.
Therefore, it supports the getText( ), setText( ), getSelectedText(
), select( ), isEditable( ), and setEditable( ) methods as of
TextField.
TextArea adds the following methods:
void append(String str)
void insert(String str, int index)
void replaceRange(String str, int startIndex, int
endIndex)
Layout Managers
All components that we have seen so far have been positioned by the
default layout manager
Layout manager automatically arranges AWT controls within a window
by using some type of algorithm.
It is possible to layout controls manually, but generally avoided due to:
FlowLayout
BorderLayout
GridLayout
CardLayout
GridBagLayout
FlowLayout
FlowLayout is the default layout manager.
Menu( )
Menu(String optionName)
MenuItem( )
MenuItem(String itemName)
They are similar to frame windows, except that dialog boxes are
always child windows of a top-level window.
Dialog boxes don’t have menu bars.
In constructor of Extended Dialog class, use super method and pass values
to constructor of Dialog
import java.awt.*;
import java.awt.event.*;
public class DialogExample {
private static Dialog d;
DialogExample() {
Frame f= new Frame();
d = new Dialog(f , "Dialog Example", true);
d.setLayout( new FlowLayout() );
Button b = new Button ("OK");
b.addActionListener ( new ActionListener()
{ public void actionPerformed( ActionEvent e )
{ DialogExample.d.setVisible(false); }
});
d.add( new Label ("Click button to continue."));
d.add(b); d.setSize(300,300);
d.setVisible(true); }
FileDialog
Java provides a built-in dialog box that lets the user specify a file.
To create a file dialog box, instantiate an object of type
FileDialog.
Usually this is standard dialog box provided by OS
Constructor:
FileDialog(Frame parent, String boxName)
FileDialog(Frame parent, String boxName, int how)
FileDialog(Frame parent)
Int how: FileDialog.LOAD, FileDialog.SAVE
Methods:
String getDirectory( )
String getFile( )
import java.awt.*;
class SampleFrame extends Frame
{
SampleFrame(String title){
super(title); }}
class FileDialogDemo
{public static void main(String args[]){
Frame f = new SampleFrame("File Dialog Demo");
f.setVisible(true);
f.setSize(100, 100);
FileDialog fd = new FileDialog(f, "File Dialog");
fd.setVisible(true);
}}