Professional Documents
Culture Documents
Abstract Windowing
Toolkit
Marks :12 Marks
Created By: Prof.P.M.Takate
Introduction to AWT
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. It can
have other components like button, textfield etc.
Frame
The Frame is the container that contain title bar and can have menu bars. It
can have other components like button, textfield etc
A frame provides main window for the GUI application which has title
bar,menu bar and the content display area.
It is AWT class that creates top level window with title and border.
Constructors:
1. Frame() : Creates window that does not contain a title.
2. Frame(String Title) : The second form creates a window with the
title specified by title.
Two ways of Creating Frame class:
1.By Instantiating Frame class(by creating object of frame class) :
Example: Output:
import java.awt.*;
class FrameDemo{
FrameDemo()
{
Frame f=new Frame() ;
Button b=new Button("click me");
b.setBounds(30,50,80,30);
f.add(b);
f.setTitle(“FrameDemo”);
f.setBackground(Color.RED);
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String args[]){
FrameDemo f=new FrameDemo();
}}
2.By extending Frame class :
Example: Output:
import java.awt.*;
class FrameDemo extends Frame{
FrameDemo()
{
Button b=new Button("click me");
b.setBounds(30,100,80,30);// setting button position
add(b);//adding button into frame
setSize(300,300);//frame size 300 width and 300 height
setVisible(true);//now frame will be visible, by default not visible
}
public static void main(String args[]){
FrameDemo f=new FrameDemo();
}}
Panel
The class panel is the simplest Button b2=new Button("Button 2");
container class.
b2.setBounds(100,100,80,30);
It provides space in which an
b2.setBackground(Color.green);
application can attach any other
component,including other panels. panel.add(b1);
Example: panel.add(b2);
import java.awt.*; f.add(panel);
public class PanelExample { f.setSize(400,400);
PanelExample()
{ f.setVisible(true);
Frame f= new Frame("Panel Example"); }
Panel panel=new Panel(); public static void main(String args[])
panel.setBounds(40,80,200,200); {
panel.setBackground(Color.gray); PanelExample p1= new PanelExample();
Button b1=new Button("Button 1"); }
b1.setBounds(50,100,80,30);
}
b1.setBackground(Color.yellow);
Output:
Output:
Creating Windowed Programs and
Applets
In previous section, The first two works implicitly and the close does not work
and requires extra code to close frame.
To solve this problem one solution is to create a Frame window within an
applet window.
Program for Frame in Applet }
import java.applet.Applet; public void paint(Graphics g)
import java.awt.*; {
public class AppletFrame extends Applet g.drawString(“This is applet
window”,10,20);
{
}
SampleFrame f;
}
public void init()
class SampleFrame extends Frame{
{
SampleFrame(String title)
f=new SampleFrame(“A frame
window”); {
f.setSize(250,250); super(title);
f.setVisible(true); }
} public void paint(Graphics g)
public void start() {
{ g.drawstring(“This is frame
window”,10,40);
f.setVisible(true);
}
}
}
public void stop()
/*<applet code=“AppletFrame.class”
{
height=200 width=200>
f.setVisible(false);
</applet> */
Output:
AWT Controls
Controls are the components that allow user to interact with java application
in various ways.
The AWT supports various types of controls such as Labels, Push Buttons,
Check Boxes, Choice Lists, Lists ,Scroll Bars,TextArea,TextField.
Labels
1 String getText()
Gets the text of this label.
2 void setText(String text)
Sets the text for this label to the specified text.
3 int getAlignment()
Gets the current alignment of this label.
4 void setAlignment(int alignment)
Sets the alignment for this label to the specified alignment.
Program
import java.awt.*;
class LabelExample{
public static void main(String args[]){
Frame f= new Frame("Label Example");
Label l1,l2;
l1=new Label("First Label.");
l1.setBounds(50,100, 100,30);
l2=new Label("Second Label.");
l2.setBounds(50,150, 100,30);
f.add(l1); f.add(l2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
Button
Button class is used to create a push button control,which can generate an
ActionEvent when it is clicked or presses.
Contructors :
2 Button(String text)
Constructs a new button with specified label.
Methods:
1 String getActionCommand()
Returns the command name of the action event fired by this button.
2 String getLabel()
Gets the label of this button.
3 void setActionCommand(String command)
Sets the command name for the action event fired by this button.
4 void setLabel(String label)
Sets the button's label to be the specified string.
Program
import java.awt.*;
public class ButtonExample {
public static void main(String[] args) {
Frame f=new Frame("Button Example");
Button b=new Button("Click Here");
b.setBounds(50,100,80,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Checkbox
The Checkbox class is used to create a checkbox.
It is used to turn an option on (true) or off (false).
Clicking on a Checkbox changes its state from "on" to "off" or from "off" to
"on".
Contructors:
S.N Constructor & Description
1 Checkbox()
Creates a check box with an empty string for its label.
2 Checkbox(String label)
Creates a check box with the specified label.
3 Checkbox(String label, boolean state)
Creates a check box with the specified label and sets the specified state.
4 Checkbox(String label, boolean state, CheckboxGroup group)
Constructs a Checkbox with the specified label, set to the specified state, and in
the specified check box group.
1 String getLabel()
Gets the label of this check box.
2 void setLabel(String label)
Sets this check box's label to be the string argument.
3 boolean getState()
Determines whether this check box is in the on or off state.
4 void setState(boolean state)
Sets the state of this check box to the specified state.
Program new CheckboxExample();
import java.awt.*; }
public class CheckboxExample
{ }
CheckboxExample(){ Output :
Frame f= new Frame("Checkbox Example");
Checkbox c1 = new Checkbox("C++");
checkbox1.setBounds(100,100, 50,50);
Checkbox c2 = new Checkbox("Java", true);
checkbox2.setBounds(100,150, 50,50);
f.add(c1);
f.add(c2);
f.setSize(400,400);
f.setVisible(true);
}
public static void main(String args[])
{
CheckboxGroup(Radio Button)
The object of CheckboxGroup class is used to group together a set of
Checkbox.
At a time only one check box button is allowed to be in "on" state and
remaining check box button in "off" state.
Constructors :
S.N. Constructor & Description
1 CheckboxGroup()
Creates a new instance of CheckboxGroup.
Methods
S.N Method & Description
1 Checkbox getSelectedCheckbox()
Gets the current choice from this check box group.
import java.awt.*; new CheckboxGroupExample();
public class CheckboxGroupExample }
{ }
CheckboxGroupExample(){ Output:
Frame f= new Frame("CheckboxGroup Example");
CheckboxGroup cbg = new CheckboxGroup();
Checkbox c1 = new Checkbox("C++", cbg, false);
c1.setBounds(100,100, 50,50);
Checkbox c2 = new Checkbox("Java",true,cbg);
c2.setBounds(100,150, 50,50);
f.add(c1);
f.add(c2);
f.setSize(400,400);
f.setVisible(true);
}
public static void main(String args[])
Choice
Choice control is used to show pop up menu of choices from which one item
can be choosed.
Selected choice is shown on the top of the menu.
Contructors :
1 Choice()
Creates a new choice menu.
Methods :
S.N. Method & Description
1 void addItem(String item)/void add(String item)
Sets the selected item in this Choice menu to be the item at the specified position.
7 void select(String str)
Sets the selected item in this Choice menu to be the item whose name is equal to the
specified string.
Program : public static void main(String args[])
import java.awt.*; {
public class ChoiceExample new ChoiceExample();
{ }
ChoiceExample(){ }
Frame f= new Frame(); Output:
Choice c=new Choice();
c.setBounds(100,100, 75,75);
c.add("Item 1");
c.add("Item 2");
c.add("Item 3");
c.add("Item 4");
c.add("Item 5");
f.add(c);
f.setSize(400,400);
f.setVisible(true);
}
List
The object of List class represents a list of text items.
By the help of list, user can choose either one item or multiple items.
Consrtructors:
1 List()
Creates a new scrolling list.
2 List(int rows)
Creates a new scrolling list initialized with the specified number
of visible lines.
3 List(int rows, boolean multipleMode)
Creates a new scrolling list initialized to display the specified
number of rows.
Methods:
S.N. Method & Description
1 void add(String item)
Adds the specified item to the the scrolling list at the position indicated by the index.
3 int getItemCount()
1 TextField()
Constructs a new text field.
2 TextField(int columns)
Constructs a new empty text field with the specified number of columns.
3 TextField(String text)
Constructs a new text field initialized with the specified text.
Indicates whether or not this text field has a character set for echoing
Program :
import java.awt.*; Output :
class TextFieldExample{
public static void main(String args[]){
Frame f= new Frame("TextField Example");
TextField t1,t2;
t1=new TextField("Welcome to AJP.");
t1.setBounds(50,100, 200,30);
t2=new TextField( );
t2.setBounds(50,150, 200,30);
f.add(t1); f.add(t2);
f.setSize(400,400);
f.setVisible(true);
}
}
TextArea
The object of a TextArea class is a multi line region that displays text.
It allows the editing of multiple line text.
Constructors :
Inserts the specified text at the specified position in this text area.
6 void replaceRange(String str, int start, int end)
Replaces text between the indicated start and end positions with the specified
replacement text.
Program
import java.awt.*; Output :
public class TextAreaExample
{
TextAreaExample(){
Frame f= new Frame();
TextArea area=new TextArea(“Welcome to javatpoint”);
area.setBounds(10,30, 300,300);
f.add(area);
f.setSize(400,400);
f.setVisible(true);
}
public static void main(String args[])
{
new TextAreaExample();
}
}
Scrollbar
The object of Scrollbar class is used to add horizontal and vertical scrollbar.
Scrollbar is a GUI component allows us to see invisible number of rows and
columns.
Constructors :
S.N. Constructor & Description
1 Scrollbar()
Constructs a new scroll bar.
2 Scrollbar(int orientation)
Constructs a new scroll bar with the specified orientation.
Orientation specified by constants Scrollbar.HORIZONTAL or
Scrollbar.VERTICAL.
3 Scrollbar(int orientation, int value, int thumbsize, int
minimum, int maximum)
Constructs a new scroll bar with the specified orientation,
initial value, thumbsize, and minimum and maximum values.
Methods :
1 int getMaximum()
Constructors :
GridBagLayout(): It is used to creates a grid bag layout manager.
Instance variables to manipulate GridBagLayout object constraints :
1.gridx and gridy
Theses contains the coordinates of the origin of the grid.They allow at specific position of a
component positioning.
2.gridwidth,gridheight
Define how many cell will occupy component(height and width).By default is 1.
3.fill
Defines how to resize the component which is smaller than the grid cell.
GridBagConstraints.NONE retains original size.
GridBagConstraints.HORIZONTAL expanded horizontally.
GridBagConstraints.VERTICAL expanded vertically.
GridBagConstraints.BOTH expanded to the dimentions of the cell.
4.ipadx,ipady
Used to define the horizontal and vertical expansion of components.
5.weightx,weighty
Used to define the distribution of space in case of change of dimention.
import java.awt.*;
public class GridBagLayoutExample{ f.add(new Button("Button Three"), gbc);
GridBagLayoutExample() { gbc.gridx = 1;
Frame f=new Frame("GridBag Layout Example"); gbc.gridy = 1;
GridBagLayout layout = new GridBagLayout(); f.add(new Button("Button Four"), gbc);
GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0;
f.setLayout(layout); gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL; gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0; gbc.gridwidth = 2;
gbc.gridy = 0; f.add(new Button("Button Five"), gbc);
Button b1=new Button (“Button One”); f. setSize(300, 300);
f.add(b1, gbc); f.setVisible(true);
gbc.gridx = 1; }
gbc.gridy = 0; public static void main(String[] args) {
f.add(new Button("Button two"), gbc); new GridBagLayoutExample();
gbc.fill = GridBagConstraints.HORIZONTAL; }
gbc.ipady = 20; }
gbc.gridx = 0;
Output:
Menu Bars
The bar which is next to title bar which has got different menus is menu bar.
Each menu contains menu items.
Example ,On menu bar you may find menus like File,Edit,View,etc.
The menu item of file could be New,Open,Save etc.
Constructor of MenuBar :
1 MenuBar()
Creates a new menu bar.
1 MenuItem()
Constructs a new MenuItem with an empty label and no
keyboard shortcut.
2 MenuItem(String label)
Constructs a new MenuItem with the specified label and no
keyboard shortcut.
3 MenuItem(String label, MenuShortcut s)
Create a menu item with an associated keyboard shortcut.
Methods :
1 Dialog(Frame owner)
Constructs an initially invisible, modeless Dialog with the
specified owner Frame and an empty titl
2 Dialog(Frame owner, boolean modal)
Constructs an initially invisible Dialog with the specified owner
Frame and modality and an empty title.
3 Dialog(Frame owner, String title)
Constructs an initially invisible, modeless Dialog with the
specified owner Frame and title.
4 Dialog(Frame owner, String title, boolean modal)
Constructs an initially invisible Dialog with the specified owner
Frame, title and modality.
Program
import java.awt.*; }
public class DialogDemo extends Dialog { Output:
DialogDemo(Frame parent,String title)
{
super(parent,title);
setLayout(new FlowLayout());
setSize(300,300);
setBackground(Color.YELLOW);
Button b=new Button("cancel");
add(b);
}
public static void main(String args[]) {
Frame f1=new Frame();
DialogDemo dd=new DialogDemo(f1,"hello");
dd.setVisible(true);
}
File Dialog
FileDialog control represents a dialog window from which the user can select
a file.
Contructors :