You are on page 1of 70

Unit I

Abstract Windowing
Toolkit
Marks :12 Marks
Created By: Prof.P.M.Takate
Introduction to AWT

 Abstract Window Toolkit is collection of classes and


interfaces.
 AWT is API to develop GUI or window based application in
java.
 AWT provides a huge set of reusable GUI Components such as
Texfield,button,label,etc.
 AWT has commonly used packages are:
1. java.awt package
2. java.awt.event package
AWT Classes
1.AWTEvent
2.Button
3.Label
4.TextField
5.TextArea
6.Checkbox
7.CheckboxGroup
8.Frame
9.Window
10.Dialog
11.FileDialog
12.FlowLayout
13.BorderLayout
14.CardLayout
Component,Container,Window,Frame,
Panel

Fig.Java AWT Hierarchy


 Component:
At the top of AWT hierarchy is component class.Component is an
abstract class that encapsulates all the attributes of visual
component.All UI elements that are displayed on screen and that
interact with user are subclasses of component.

Sr.No Methods Description


1 void add(Component c) Add a component
2 void setSize(int width,int height) Sets size of component
3 void setLayout(LayoutManager m) Sets the Layout Manager
for component
4 void setVisible(Boolean b) Sets the visibility of
component.It is by
default false.
5 void remove(Component c) Remove Component
6 void setBounds(int x,int y,int Used to set location and
width,int height) size of single
component
 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.

Sr.No Methods Description


1 void setFont(Font f) Sets the font of this
container
2 void setLayout(LayoutManager mgr) Sets the layout Manager for
this container

 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

Sr.No Methods Description


1 void setTitle(String title) Sets the title for the frame
2 void setBackground(Color bgColor) Sets the Background Color of
Frame
Frame Class

 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

 A label displays a single line of read only text.


 A label is an object of type Label and it contains a string, which it displays.
 Constructors :

SR.No Constructor & Description


1 Label()
Constructs an empty label.
2 Label(String text)
Constructs a new label with the specified string of text, left justified.
3 Label(String text, int alignment)
Constructs a new label that presents the specified string of text with
the specified alignment.
Label.LEFT Label.RIGHT,Label.CENTER
 Methods:

S.N. Method & Description

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 :

S. Constructor & Description


N.
1 Button()
Constructs a button with an empty string for its label.

2 Button(String text)
Constructs a new button with specified label.
 Methods:

S.N. Method & Description

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.

5 Checkbox(String label, CheckboxGroup group, boolean state)


Creates a check box with the specified label, in the specified check box group, and
set to the specified state.
 Methods

S.N. Method & Description

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.

2 void setSelectedCheckbox(Checkbox box)


Sets the currently selected check box in this group to be the specified check
box.
 Program : {    

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 :

S.N. Constructor & Description

1 Choice()
Creates a new choice menu.
 Methods :
S.N. Method & Description
1 void addItem(String item)/void add(String item)

Used to add item in choice menu.


2 String getItem(int index)

Gets the string at the specified index in this Choice menu.


3 int getItemCount()

Returns the number of items in this Choice menu.


4 int getSelectedIndex()

Returns the index of the currently selected item.


5 String getSelectedItem()

Gets a representation of the current choice as a string.


6 void select(int pos)

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:

S.N. Constructor & Description

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 end of scrolling list.


2 void add(String item, int index)

Adds the specified item to the the scrolling list at the position indicated by the index.
3 int getItemCount()

Gets the number of items in the list.


4 String getItem(int index)

Gets the item associated with the specified index.


5 int getSelectedIndex()

Gets the index of the selected item on the list,


6 int[] getSelectedIndexes()

Gets the selected indexes on the list.


7 String getSelectedItem()

Gets the selected item on this scrolling list.


8 String[] getSelectedItems()

Gets the selected items on this scrolling list.


Program:      }  
public static void main(String args[])  
import java.awt.*;  
{  
public class ListExample  
   new ListExample();  
{  
}  
     ListExample(){  

        Frame f= new Frame();  
Output : 
        List l1=new List(5,true);  
        l1.setBounds(100,100, 75,75);  
         l1.add("Item 1");  
        l1.add("Item 2");  
        l1.add("Item 3");  
        l1.add("Item 4");  
        l1.add("Item 5");  
          f.add(l1);  
        f.setSize(400,400);  
       f.setVisible(true);  
TextField
 The object of a TextField class is a text component that allows the editing of
a single line text.
 Constructor :
S.N. Constructor & Description

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.

4 TextField(String text, int columns)


Constructs a new text field initialized with the specified text to be displayed, and
wide enough to hold the specified number of columns.
 Methods :
S.N. Method & Description
1 String getText()
Gets the text of this TextField.
2 void setText(String text)
Sets the text for this TextField to the specified text.
3 void select(int startIndex,int endIndex)

The portion of the text can be selected from the TextField.


4 String getSelectText()

This method returns currently selected text in TextField.


5 void setEchoChar(char c)

Sets the echo character for this text field.


6 char getEchoChar()

Gets the character that is to be used for echoing.


7 boolean echoCharIsSet()

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 :

S.N. Constructor & Description


1 TextArea()
Constructs a new text area with the empty string as text.
2 TextArea(int rows, int columns)
Constructs a new text area with the specified number of rows and
columns and the empty string as text.
3 TextArea(String text)
Constructs a new text area with the specified text.
4 TextArea(String text, int rows, int columns)
Constructs a new text area with the specified text, and with the
specified number of rows and columns.
5 TextArea(String text, int rows, int columns, int scrollbars)
Constructs a new text area with the specified text, and with the
rows, columns, and scroll bar visibility as specified.
 Scrollbar Visibility Constants :

1. static int SCROLLBARS_BOTH -- Create and display both vertical and


horizontal scrollbars.
2. static int SCROLLBARS_HORIZONTAL_ONLY -- Create and display horizontal
scrollbar only.
3. static int SCROLLBARS_NONE -- Do not create or display any scrollbars for
the text area.
4. static int SCROLLBARS_VERTICAL_ONLY -- Create and display vertical
scrollbar only.
 Methods:
S.N. Method & Description

1 void append(String str)

Appends the given text to the text area's current text.


2 int getColumns()

Returns the number of columns in this text area.


3 void setColumns(int columns)

Sets the number of columns for this text area.


4 int getRows()

Returns the number of rows in the text area.


5 void insert(String str, int pos)

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 :

S.N. Method & Description

1 int getMaximum()

Gets the maximum value of this scroll bar.


2 int getMinimum()

Gets the minimum value of this scroll bar.


3 void setValue(int newValue)

Sets the value of this scroll bar to the specified value.


4 int getValue()

Gets the current value of this scroll bar.


Program
import java.awt.*;   Output:
class ScrollbarExample{  
ScrollbarExample(){  
            Frame f= new Frame("Scrollbar Example");  
            Scrollbar s=new Scrollbar();  
            s.setBounds(100,100, 50,100);  
            f.add(s);  
            f.setSize(400,400);  
     f.setVisible(true);  
}  
public static void main(String args[]){  
       new ScrollbarExample();  
}  
}  
Layout Manager
 Layout means the arrangement of components within the container.
 The task of layouting the controls are done automatically by the Layout Manager.
 The layout manager is set by the setLayout() method.
 If no call to setLayout() is made,then the default layout manager is used.
 The setLayout having following syntax :
void setLayout(LayoutManager layoutobj)
 Here,layoutObj is a reference to the desired layout manager.
 Java has several layout manager classes.
 They are founded in the java.awt package same as the classes used for displaying
graphical components.
 Types of Layout Managers :
1.FlowLayout
2.BorderLayout
3.GridLayout
4.CardLayout
5.GridBagLayout
FlowLayout
 FlowLayout is the default layout manager.
 Components are laid out from the upper-left corner,left to right and top to
bottom.
 When no more components fit on line,the next one appears on the next line.
 Small space is left between each component,above and below,as well as left and
right.
 Constructors :
1 FlowLayout(): creates a flow layout with centered alignment and a default 5 unit
horizontal and vertical gap.
2 FlowLayout(int align): creates a flow layout with the given alignment and a default
5 unit horizontal and vertical gap.
3 FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given
alignment and the given horizontal and vertical gap.
Alignment Constants:
 FlowLayout.LEFT
 FlowLayout.RIGHT
Program
import java.awt.*;   f.add(b4);
public class MyFlowLayout{   f.add(b5);  
  f.setSize(300,300);  
MyFlowLayout(){    f.setVisible(true);  
Frame f=new Frame();  }  
FlowLayout f1=new FlowLayout();  public static void main(String[] args) {  
      f.setLayout(f1);       new MyFlowLayout();  
    Button b1=new Button("1");   }  
    Button b2=new Button("2");   } 
    Button b3=new Button("3");  Output :
    Button b4=new Button("4");  
    Button b5=new Button("5");  
 f.add(b1);
f.add(b2);
f.add(b3);
BorderLayout
 The BorderLayout class implements a common layout style for top level
windows.
 It has four narrow,fixed width components at edges and one large area in the
center.
 The four sides are referred to as north(upper region),south(lower
region),east(left region),west(right region).
 The middle is called the center(central region).
 Constructors:
1 BorderLayout(): creates a border layout but with no gaps between the
components.
2 BorderLayout(int hgap, int vgap): creates a border layout with the given
horizontal and vertical gaps between the components.
Program
import java.awt.*;       f.add(b5,BorderLayout.CENTER);    
public class BorderLayoutExample {        f.setSize(300,300);  
BorderLayoutExample (){       f.setVisible(true);  
    Frame f =new Frame();   }  
BorderLayout b=new BorderLayout(); public static void main(String[] args) {  
f.setLayout(b);     new BorderLayoutExample ();  
    Button b1=new Button("NORTH"); }  
    Button b2=new Button("SOUTH");   }  
    Button b3=new Button("EAST");  Output:
    Button b4=new Button("WEST");  
    Button b5=new Button("CENTER");
    f.add(b1,BorderLayout.NORTH);  
    f.add(b2,BorderLayout.SOUTH);  
    f.add(b3,BorderLayout.EAST);  
    f.add(b4,BorderLayout.WEST);  
GridLayout
 GridLayout lays out components in a two dimentional grid.
 When we instantiate a GridLayout,we define the number of rows and
columns.
 GridLayout manager places items in rows and columns.
 Constructors:
1.GridLayout(): creates a grid layout with one column per component in a row.
2.GridLayout(int rows, int columns): creates a grid layout with the given rows
and columns but no gaps between the components.
3.GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout
with the given rows and columns alongwith given horizontal and vertical gaps.
Program
import java.awt.*;           Button b9=new Button("9");         
public class MyGridLayout{       f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.a
dd(b5);  
MyGridLayout(){  
    f.add(b6);f.add(b7);f.add(b8);f.add(b9); 
     Frame f=new Frame(); 
    f.setSize(300,300);  
GridLayout g=new GridLayout(3,3); 
    f.setVisible(true);  
f.setLayout(g); 
}  
    Button b1=new Button("1");  
public static void main(String[] args) {  
    Button b2=new Button("2");  
    new MyGridLayout();  
    Button b3=new Button("3");  
}  
    Button b4=new Button("4");  
}  
    Button b5=new Button("5");  
Output:
     Button b6=new Button("6");  
     Button b7=new Button("7");  
    Button b8=new Button("8");  
Card Layout
 The CardLayout class manages the components in such a manner that only one
component is visible at a time.
 It treats each component as a card that is why it is known as CardLayout.
 Constructors :
1.CardLayout(): creates a card layout with zero horizontal and vertical gap.
2.CardLayout(int hgap, int vgap): creates a card layout with the given
horizontal and vertical gap.
 Methods :
 public void next(Container parent): is used to flip to the next card of the given
container.
 public void previous(Container parent): is used to flip to the previous card of the
given container.
 public void first(Container parent): is used to flip to the first card of the given
container.
 public void last(Container parent): is used to flip to the last card of the given
container.
 public void show(Container parent, String name): is used to flip to the specified card
with the given name.
Program
import java.awt.*; f.setVisible(true);
class CardLayoutDemo }
{ public static void main(String[] args) {  
CardLayoutDemo()     new CardLayoutDemo();  
{ }  
Frame f=new Frame(); }  
CardLayout card=new CardLayout(20,20); Output:
f.setLayout(card);
Button b1=new Button(“Apple”);
Button b2=new Button(“Orange”);
Button b3=new Button(“Mango”);
f.add(b1,”card1”);
f.add(b2,”card2”);
f.add(b3,”card3”);
f.setSize(400,400);
GridBagLayout
 The Java GridBagLayout class is used to align components vertically,
horizontally or along their baseline.
 The components may not be of same size.
 Each GridBagLayout object maintains a dynamic, rectangular grid of cells.
Each component occupies one or more cells known as its display area.
 Each component associates an instance of GridBagConstraints.
 With the help of constraints object we arrange component's display area on
the grid.

 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 :

S.N Constructor & Description

1 MenuBar()
Creates a new menu bar.

 Method to set Menu Bar:


Syntax:
setManuBar(instance of Menubar);
Menu
 The Menu class represents pull-down menu component which is deployed
from a menu bar.
 Contructors :

S.N. Constructor & Description


1 Menu()
Constructs a new menu with an empty label.
2 Menu(String label)
Constructs a new menu with the specified label.
3 Menu(String label, boolean tearOff)
Constructs a new menu with the specified label, indicating
whether the menu can be torn off.
MenuItem
 Each Menu object contains a list of MenuItem Objects.
 Each MenuItem Object represents something that can be selected by the user.
 Contructors:

S.N. Constructor & Description

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 :

S.N Method & Description


1 String getLabel()
Gets the label for this menu item.
2 boolean isEnabled()
Checks whether this menu item is enabled.
3 void setEnabled(boolean b)
Sets whether or not this menu item can be chosen.
4 void setLabel(String label)
Sets the label for this menu item to the specified label.
CheckboxMenuItem
 We can create checkable menu item by using a class CheckboxMenuItem,which
is a subclass of menu item.
 Constructors :

S.N Constructor & Description


1 CheckboxMenuItem()
Create a check box menu item with an empty label.
2 CheckboxMenuItem(label)
Create a check box menu item with the specified label.
3 CheckboxMenuItem(label, boolean state)
Create a check box menu item with the specified label and
state.
 Methods :

S.N Method & Description


1 void setState(boolean b)
Sets this check box menu item to the specifed state.
2 boolean getState()
Determines whether the state of this check box menu
item is "on" or "off."
Program
import java.awt.*;            menu.add(i3);  
class MenuExample            submenu.add(i4);  
{            submenu.add(i5);  
     MenuExample(){            menu.add(submenu);  
 Frame f= new Frame("Menu and MenuItem          mb.add(menu);  
Example");  
         f.setMenuBar(mb);  
         MenuBar mb=new MenuBar();  
         f.setSize(400,400);  
         Menu menu=new Menu("Menu");  
         f.setLayout(null);  
         Menu submenu=new Menu("Sub Menu");  
         f.setVisible(true);  
         MenuItem i1=new MenuItem("Item 1");  
}  
         MenuItem i2=new MenuItem("Item 2");  
public static void main(String args[])  
         MenuItem i3=new MenuItem("Item 3");  
{  
         MenuItem i4=new MenuItem("Item 4");  
new MenuExample();  
         MenuItem i5=new MenuItem("Item 5");  
}  
         menu.add(i1);  
}  
         menu.add(i2);  
 Output :
Dialog Boxes
 A Dialog box is a GUI object in which you can place messages that you want to
display on the screen.
 Dialog boxes are always child windows of a top-level windows.
 Dialog boxes may be modal or modeless.
 When a modal dialog box is active,we cannot access other parts of our
program until we have closed the dialog box.
 When modeless dialog box is active,input focus can be directed to another
window in our program.
 Constructors :

S.N. Method & Description

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 :

S.N Method & Description


1 FileDialog(Frame parent)
Creates a file dialog for loading a file.
2 FileDialog(Frame parent, String title)
Creates a file dialog window with the specified title for
loading a file.
3 FileDialog(Frame parent, String title, int mode)
Creates a file dialog window with the specified title for
loading or saving a file.
Program
import java.awt.*; Dialog");
class SampleFrame extends Frame fd.setVisible(true);
{ }
SampleFrame(String title) }
{ Output :
super(title);
}
}
public class FileDialogDemo {
public static void main(String args[])
{
SampleFrame f=new SampleFrame("File
Dialog Demo");
f.setVisible(true);
f.setSize(100,100);
FileDialog fd=new FileDialog(f,"File
Thank You

You might also like