0% found this document useful (0 votes)
4 views

UNIT 1 - AWT (Abstract Windowing Toolkit)

all awt explaition in this

Uploaded by

khotakash556
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

UNIT 1 - AWT (Abstract Windowing Toolkit)

all awt explaition in this

Uploaded by

khotakash556
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 83

INTRODUCTION TO ABSTRACT WINDOWING

TOOLKIT(AWT)
12 Marks
UNIT 1- AWT(Abstract windowing toolkit)

• Java AWT or Abstract Window Toolkit is an API used for


developing GUI(Graphic User Interfaces) or Window-Based
Applications in Java
• Java AWT is part of the Java Foundation Classes (JFC) that
provides a way to build platform-independent graphical
applications.
• The java.awt package contains AWT API classes such
as TextField, Label, TextArea, RadioButton, CheckBox, Choice,
List etc.
• AWT is heavyweight, which means that its components
consume resources from the underlying operating system
(OS)
AWT is Platform Independent?
• The Java AWT utilizes the native platform subroutine to create API
components such as TextField, CheckBox, and buttons.
• This results in a different visual format for these components on different
platforms such as Windows, MAC OS, and Unix.
• The reason for this is that each platform has a distinct view of its native
components

• Java AWT Hierarchy


 Container
➢ Container is a component in AWT that contains
another component like button, text field, tables etc.
➢ Container is a subclass of component class. Container
class keeps track of components that are added to
another component.
➢ A container is responsible for laying out (that is
positioning) any components that it contains.
➢ It does this through the use of various layout managers.
 Panel
➢ Panel class is a concrete subclass of Container.
➢ Panel is the super-class for Applet. When screen
output is directed to an applet, it is drawn on the
surface of a Panel object.
➢ Panel does not contain title bar, menu bar or
border. It is container that is used for holding
components.
➢ To create panel,
Panel p=new panel();
 Window
➢ The Window class creates a top-level window.
➢ A top -level window is not contained withinany
other object; it sits directly on the desktop.
➢ Generally, we won’t create Window objects
directly. Instead, we will use a subclass of
Window called Frame.
 Canvas
➢ Canvas is not there in the hierarchy but still it exists

➢ Canvas encapsulates a blank window upon which we can draw.


Frame
• Frame is a subclass of Window and have resizing canvas. It is a container that
contain several different components like button, title bar, textfield, label etc. In
Java, most of the AWT applications are created using Frame window.
• Frame class has two different constructors

• Frame() throws HeadlessException


• Frame(String title) throws HeadlessException

Creating a Frame
There are two ways to create a Frame. They are,
• By Instantiating Frame class
• By extending Frame class
AWT Classes
AWT Classes
AWT Classes
AWT Classes
AWT Classes
➢Setting a Window’s Title
 void setTitle(String newTitle)

➢ Closing a Frame Window


setVisible(false).
• you must implement the windowClosing( ) method of
the WindowListener interface
• Inside windowClosing( ), you must remove the window
from the screen
AWT CONTROLS
 The controls are different components which allows a
user to interact with our application in various wayss.
 For adding contols in a window,first create an instance
of the required control and then add it to a window by
calling add() method.
 Syntax

Component add(Component compObj)


Here, compObj is an instance of the control that we
want to add.
 For removing controls from a window,call
remove() method
 Syntax

void remove(Component obj)


 Here, obj is a reference to the control that we want
to remove. We can remove all controls by calling
removeAll( ).
TYPES OF CONTROLS
 Passive Control
This are the control which do not generate any
event Ex Label
 Active Control
This are the control which generate certain
event Ex Button,List,Scrollbar etc
AWT CONTROLS
 Label
 Button

 Checkbox

 Checkbox Group

 Choice

 List

 Scrollbar

 TextField

 TextArea
LABEL
 Labels are passive controls that do not support any
interaction with the user.
 Constructors

Label( )
Label(String str)
Label(String str, int how)
Where as
str- String to display which is left aligned. how-
how the alignment. I
t’s value can be set as,
Label.LEFT, Label.RIGHT,
Label.CENTER
 Methods
1) void setText(String str)

2) String getText()

3) void setAlignment(int how) : Sets the alignment


of the string within the label.
4)int getAlignment() : Allow you to set the
alignment and also to get the the current
alignment.
PROGRAM TO DEMONSTRATE USE OF LABEL
import java.awt.*;

public class LabelEx


{
public static void main(String[] args)
{
Label one=new Label("ONE", Label.LEFT);
Label two=new Label("TWO");
Frame f=new Frame("Label Frame");
f.add(one);
f.add(two);
f.setVisible(true);
f.setSize(500,300);
one.setBounds(80, 80, 50, 20);
two.setBounds(80,80,30,20);
}
}
Points to Remember:
• While creating a frame (either by instantiating or
extending Frame class),
• Following two attributes are must for visibility of the
frame:
– setSize(int width, int height);
– setVisible(true);
• When you create other components like Buttons,
TextFields, etc.
• Then you need to add it to the frame by using the
method - add(Component's Object);

• You can add the following method also for resizing the
frame - setResizable(true);
setBounds() method

• The setBounds() method is used to set the x and y


coordinates, as well as the width and height of a component.
Its signature is as follows:
• void setBounds(int x, int y, int width, int height)
• x: The x-coordinate of the component's top-left corner.
• y: The y-coordinate of the component's top-left corner.
• width: The width of the component.
• height: The height of the component.
• AWT Button
• In Java, AWT contains a Button Class. It is used
for creating a labeled button which can
perform an action.
BUTTON
 It is a component which has a label andit
generates an event when it is pressed.
 Constructors:

Button( )
Button(String str)
 Methods:
void setLabel(String str)
String getLabel()
Creating Frame window by extending Frame class

• import java.awt.Button;
• import java.awt.Frame;

• public class ButtonEx extends Frame


• {
• ButtonEx()
• {
• Button y=new Button("YES");
• Button n=new Button("NO");
• Button MN=new Button("MAY not decided");
• add(y);
• add(n);
• add(MN);
• setSize(200,200);
• y.setBounds(50,80,40,30);
• MN.setBounds(50,100,40,30);
• n.setBounds(50,120,40,30);
• setVisible(true);
• }
• public static void main(String[] args)
• {
• ButtonEx b=new ButtonEx();
• }
• }
TEXT FIELD
 The TextField class implements a single-line text- entry
area, usually called an edit control. 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 is a subclass of TextComponent.
 Constructors:
1) TextField() : Default text field.
2) TextField(int numChars) : specifies the number
of characters it can hold.
3) TextField(String str) : It initializes the textfield
with the string specified.
4) TextField(String str, int numChars) : initializes a
textfield and sets it width.
▪ Events:
TextListener textChanged() TextEvent
 Methods:
1) String getText() : to obtain the string currently in the textfield.

2) void setText(String str) : to set the text.

3) Void select(int startindex,int endindex) : you can select portion of text under program
control.

4) String getSelectedText() : to obtain the currently selected text.

5)Void setEditable(boolean edit) : allows you to control a textfield may be modified by

the user. void setEditable(Boolean canEdit)

6)isEditable() : to deteremine whether it is editable or not.

Boolean isEditable()

7) setEchoChar() : to set echoing of character as they are typed.

void setEchoChar(char ch)

8) Char getEchoChar() : to retrieve the echochar


AWT TextField
In Java, AWT contains aTextField Class. It is used for displaying single line text.

• import java.awt.*;
• class TextFieldDemo1{
• public static void main(String args[])
• {
• Frame TextF_f= new Frame("studytonight ==>TextField");
• TextField text1,text2;
• text1=new TextField("Welcome to studytonight");
• text1.setBounds(60,100, 230,40);
• text2=new TextField("This tutorial is of Java");
• text2.setBounds(60,150, 230,40);
• TextF_f.add(text1);
• TextF_f.add(text2);
• TextF_f.setSize(500,500);
• TextF_f.setLayout(null);
• TextF_f.setVisible(true);
• }
• }

• AWT TextArea
• In Java, AWT contains aTextArea Class. It is
used for displaying multiple-line text.
TEXT AREA
 It is a multi line editor byAWT.
 Constructors:
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)
• import java.awt.*;
• public class TextAreaDemo1
• {
• TextAreaDemo1()
• {
• Frame textArea_f= new Frame();
• TextArea area=new TextArea("Welcome to studytonight.com");
• area.setBounds(30,40, 200,200);
• textArea_f.add(area);
• textArea_f.setSize(300,300);
• textArea_f.setLayout(null);
• textArea_f.setVisible(true);
• }
• public static void main(String args[])
• {
• new TextAreaDemo1();
• }
• }

• AWT Checkbox
• In Java, AWT contains a Checkbox Class. It is used
when we want to select only one option i.e true or
false. When the checkbox is checked then its state is
"on" (true) else it is "off"(false).
CHECK BOX
 A check box is a control that is used to turn an option on
or off. It consists of a small box that can either contain a
check mark or not.
 Constructors
Checkbox( )
Checkbox(String str)
Checkbox(String str, boolean on)
Checkbox(String str, boolean on, CheckboxGroup cbGroup)
Checkbox(String str, CheckboxGroup cbGroup, boolean on)
Methods
boolean getState( )
void setState(boolean on)
String getLabel( )
void setLabel(String str)
• package javacomponent;
• import java.awt.*;
• public class CheckBoxEx {

• public static void main(String[] args)


• {
• Checkbox Win98, winNT, solaris, mac;
• Frame f=new Frame("Checkbox example");
• Win98=new Checkbox("Windows 98", true);
• winNT=new Checkbox("windows NT");
• mac=new Checkbox("MAC OS");
• f.add(Win98);
• f.add(winNT);
• f.add(mac);
• Win98.setBounds(80,100,100,80);
• mac.setBounds(80,200,100,80);
• winNT.setBounds(80, 160, 100, 80);
• f.setVisible(true);
• f.setSize(600, 600);
• }

• }
CHECKBOX GROUP
 It is used to create a set of mutually exclusive check
boxes in which one and only one checkbox in the
group can be checked at any one time. These check
boxes are often called radio buttons
 Constructor
CheckboxGroup();
Methods

Checkbox getSelectedCheckbox( )
void setSelectedCheckbox(Checkbox which)
where as
which is the check box that you want to be selected.
• import java.awt.*;
• public class CheckboxGroupExample
• {
• CheckboxGroupExample()
• {
• Frame f= new Frame("CheckboxGroup Example");
• CheckboxGroup cbg = new CheckboxGroup();
• Checkbox checkBox1 = new Checkbox("Male", cbg, false);
• checkBox1.setBounds(100,100, 50,50);
• Checkbox checkBox2 = new Checkbox("Female", cbg, true);
• checkBox2.setBounds(100,150, 60,50);
• f.add(checkBox1);
• f.add(checkBox2);
• f.setSize(400,400);
• f.setLayout(null);
• f.setVisible(true);
• }
• public static void main(String args[])
• {
• new CheckboxGroupExample();
• }
• }
Java AWT Choice
• The object of Choice class is used to show popup menu of
choices. Choice selected by user is shown on the top of a
menu. It inherits Component class.
 Constructor
• Choice();
 Methods
•void add(String name)
String getSelectedItem( ) int
getSelectedIndex( ) int
getItemCount( )
• void select(int index)
• void select(String name)
• String getItem(int index)
• import java.awt.*;
• public class ChoicegrpEx
• {

• public static void main(String[] args)


• {
• // creating a frame
• Frame f = new Frame();

• // creating a choice component


• Choice c = new Choice();

• // setting the bounds of choice menu


• c.setBounds(100, 100, 75, 75);

• // adding items to the choice menu


• c.add("DEPT");
• c.add("CM");
• c.add("IT");
• c.add("CE");
• c.add("ME");

• // adding choice menu to frame


• f.add(c);

• // setting size, layout and visibility of frame


• f.setSize(400, 400);
• f.setLayout(null);
• f.setVisible(true);
• }
• }
Java AWT List
• The object of List class represents a list of text
items. With the help of the List class, user can
choose either one item or multiple items. It
inherits the Component class.
LIST
 It provides a compact, multiple-choice, scrolling
selection list.
 Constructors

List( )
List(int numRows)
List(int numRows, boolean multipleSelect)
Where as,
numRows=number of entries in the list multipleSelect=t
can be set as true or false. If true
user can select two or more items at a time.
 Methods
1) void add(String name)
2) void add(String name, int index) : adds item at
the specified index.
3) String getSelectedItem() : returns String
containing the name of the item.
4) int getSelectedIndex() : It returns the index of
the item.
5) String[] getSelectedItems() : returns an array containing
the name of currently selected items.
6)Int[] getSelectedIndexes() : returns an array containing
the indexes of currently selected items.
• import java.awt.*; • // setting size, layout and visibility of frame
• • f.setSize(400, 400);
• public class ListExample1 • f.setLayout(null);
• { • f.setVisible(true);
• // class constructor • }
• ListExample1() { •
• // creating the frame • //main method
• Frame f = new Frame(); • public static void main(String args[])
• // creating the list of 5 rows • {
• List l1 = new List(5); • new ListExample1();
• • }
• }
• // setting the position of list component
• l1.setBounds(100, 100, 75, 75);

• // adding list items into the list


• l1.add("C");
• l1.add("C++");
• l1.add("JAVA");
• l1.add("Python");
• l1.add("DBMS");

• // adding the list to frame


• f.add(l1);

SCROLL BARS
 Scrollbars allows users to scroll the components. It can
be set horizontally or vertically. The slider box can be
dragged by the user to a new position.
 Constructors:

1) Scrollbar() : creates vertical scroll bar

2) Scrollbar(int style)

3)Scrollbar(int style, int initialValue, int thumbsize, int min, int


max )
Where as
Styles:the orientation of the scroll bar. Styles can have
values :
Scrollbar.VERTICAL – creates vertical scrollbar

Scrollbar.HORIZONTAL – creates horizontal scroll bar


thumbsize’ represents number of units represented by
the height of the thumb is passed.
‘min’ and ‘max’ represents minimum and maximum
values for the scroll bar.
 Methods
1) void setValues(int initialValue, int
thumbsize, int min, int max ) : to set the
parameters.
2)int getValue() : to obtain current value of the
scroll bar.
3) void setValue(int newValue) : ‘newValue’
specifies new value for scroll bar.
4)int getMinimum():To retrieveminimum value.
5)int getMaximum():To retrievemaximum value.
• import java.awt.Frame;
• import java.awt.Scrollbar;

• public class Scrollbarexample


• {
• public static void main(String[] args)
• {
• Frame f=new Frame("ScrollBar example");
• Scrollbar s1=new Scrollbar(Scrollbar.HORIZONTAL);
• Scrollbar s2=new Scrollbar(Scrollbar.VERTICAL);
• f.add(s1);
• f.add(s2);
• f.setVisible(true);
• f.setSize(500,500);

• }
• }
LAYOUT MANAGER
 Layout means the arrangement of components
• within the container.
 The layout manager automatically positions all the
components within the container.
 The layout manager is associated with every
Container object.
 Each layout manager is an object of the class that
• implements the LayoutManager interface.
 The layout manager is set by the setLayout( ) method
• void setLayout(LayoutManager layoutObj)
TYPES OF LAYOUTMANAGER
 FlowLayout
 BorderLayout

 GridLayout

 CardLayout

 GridBagLayout
 The constructors for FlowLayout are shownbelow:
1. FlowLayout( )

It creates the default layout, which centers components


and leaves five pixels of space between each component.

2. FlowLayout(int how)
It allows you to specify how each Line is aligned.The value of how
is
 FlowLayout.LEFT
 FlowLayout.CENTER
 FlowLayout.RIGHT

3. FlowLayout(int how, int horz, int vert)


It allows specifying the horizontal and vertical space left
between components
• package LayoutExamples;

• import java.awt.*;

• public class FlowLayoutEx


• {
• public static void main(String[] args)
• {
• Frame f=new Frame("Flowayout Example");
• Button one=new Button("1");
• Button two=new Button("2");
• Button three=new Button("3");
• Button plus=new Button("+");
• Button min=new Button("-");
• f.add(one);
• f.add(two);
• f.add(three);
• f.add(plus);
• f.add(min);
• f.setVisible(true);
• f.setSize(200,200);
• f.setLayout(new FlowLayout());
• }
• }
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

 The constructors defined by BorderLayout are shown


below:
➢ BorderLayout( )

➢ BorderLayout(int horz, int vert)


 To add the control in the window
void add(Component compObj, Object region)

Here, compObj is the component to be added, and region


specifies where the component will be added.
 BorderLayout defines the following constants that
specify the regions:
BorderLayout.CENTER
BorderLayout.SOUTH
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.NORTH
• import java.awt.*;
• public class BorderLayoutEx {

• public static void main(String[] args)


• {
• Button R,L,N,S,C;
• Frame fm=new Frame();
• R=new Button("EAST");
• L=new Button("WEST") ;
• N=new Button("NORTH" );
• S=new Button("SOUTH" );
• C=new Button("CENTER");
• fm.add(L,BorderLayout.EAST);
• fm.add(R,BorderLayout.WEST);
• fm.add(N,BorderLayout.NORTH);
• fm.add(S,BorderLayout.SOUTH);
• fm.add(C,BorderLayout.CENTER);
• fm.setVisible(true);
• fm.setSize(500,500);
• // TODO Auto-generated method stub

• }

• }
Grid Layout
• The Grid Layout class is used to arrange the
components in a rectangular grid.
• One component is displayed in each rectangle
• When you instantiate a GridLayout, you
define the number of rows and columns.
 The constructors supported by GridLayout are
shown below:
➢ GridLayout( )
• It creates a single-column grid layout.

➢ GridLayout(int numRows, int numColumns )


• It creates a grid layout with the specified number
of rows and columns but no gaps between the
component
➢ GridLayout(int numRows, int numColumns, int
horz, int vert)
• It allows you to specify the horizontal and vertical
space between components
• package LayoutExamples; • f.add(b1);
• import java.awt.*; • f.add(b2);
• public class GridLayoutExample • f.add(b3);
• { • f.add(b1);
• public GridLayoutExample() • f.add(b4);
• { • f.add(b5);
• Frame f=new Frame(); • f.add(b6);
• Button b1,b2,b3,b4,b5,b6,b7,b8,b9; • f.add(b7);
• b1=new Button("1"); • f.add(b8);
• b2=new Button("2"); • f.add(b9);
• b3=new Button("3");
• b4=new Button("4"); • //f.setLayout(new GridLayout());
• b5=new Button("5");
• b6=new Button("6"); • f.setLayout(new GridLayout(3,3));//
setting gridLayout 3 rows and 3 column
• b7=new Button("7");
• f.setVisible(true);
• b8=new Button("8");
• f.setSize(300,300);
• b9=new Button("9");
• // TODO Auto-generated constructor stub
• }
• public static void main(String[] args)
• {
• new GridLayoutExample();
• }
Card Layout
• Cardlayout class manages the components in
such a manner that only one component is
visible at a time.
• The container acts as a stack of cards. The first
component added to a card layout is the
visible component when the container is first
displayed.
Constructors
➢ CardLayout()
• Creates a new card layout with gaps of size
zero
• CardLayout(int hgap, int vgap) Creates a new
card layout with the specified horizontal and
vertical gaps
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.
• package LayoutExamples;
• @Override
• import java.awt.Button; • public void
• import java.awt.CardLayout; actionPerformed(ActionEvent e)
• import java.awt.Container; • {
• import java.awt.Frame; • card.next(this);
• import java.awt.event.ActionEvent;

• import java.awt.event.ActionListener;

• public class CardLayoutEx extends Frame implements • // TODO Auto-generated
ActionListener method stub
• { •
• }
• CardLayout card;


• Button bt1, bt2,bt3,bt4;
• public static void main(String[] args)
• • {
• • CardLayoutEx frm=new CardLayoutEx();
• • frm.setSize(500,500);
• CardLayoutEx() • frm.setVisible(true);
• {
• frm.setVisible(true);

• card=new CardLayout();
• frm.setSize(500,500);
• setLayout(card); •

• bt1=new Button("CM Class"); •


• bt2=new Button("CM 1"); •

• }
• bt3=new Button("CM 2");
• bt4=new Button ("CM3");
• • }
• add(bt1);
• add(bt2);
• add(bt3);
• add(bt4);
GridBagLayout Class
 GridBagLayout places components in a grid of rows
and columns,
 allowing specified components to span multiple rows
or columns
 It doesn’t require the components of the same size
 Each GridBagLayout object manages a rectangular
grid of cells, dynamic with each component
occupying one or more cells, called its display area.

Constructor

GridBagLayout()
commonly Used Methods:

• removeLayoutComponent(Component cmp): Removes the


specified component from this layout.
• getLayoutAlignmentY(Container p): Returns the alignment along
the y-axis.
• addLayoutComponent(Component cmp, Object cons): Add the
specified component with the specified name to the layout.
• toString(): Returns a string representation of this grid bag layout’s
values.
• getLayoutAlignmentX(Container p): Returns the alignment along
the x-axis.
• getConstraints(Component cmp): Gets the constraints for the
specified component.
• maximumLayoutSize(Container tar): Returns the maximum
dimensions for this layout given the components in the specified
target container.
• minimumLayoutSize(Container par): Determines the minimum size
of the parent container using this grid bag layout.
The GridBagConstraints Class
• The appearance of a grid bag layout is controlled by sets
of GridBagConstraints
• int gridx, gridy
• Controls the position of the component on the layout’s grid.
• int weightx, weighty
• Controls how additional space in the row or column is allotted to the
component.
• int fillControls
• whether the component expands to fill the space allotted to it.
• int gridheight, gridwidth
• Controls the number of rows or columns the component spans.
• int anchor
• Controls the position of the component if there is extra room within the
space allotted to it.
• int ipadx, ipady
• Controls padding between the component and the borders of its area.
• Insets insets
• Controls padding between the component and neighboring components.
JAVA AWT MENU HIERARCH

 Menus creation involves lot of classes


• like MenuBar, Menu and MenuItem and one
is added to the other.
 MenuBar: MenuBar holds the menus.
MenuBar is added to frame
with setMenuBar()method.
Implicitly,the menu bar is added to the north
(top) of the frame.
 Menu: Menu holds the menu items.
 Menu is added to frame with add() method. A
sub- menu can be added to Menu.
 MenuItem: MenuItem displays the actual option
user can select.
 Menu items are added to menu with method
addMenuItem().
 A dull-colored line can be added in between
menu items with addSeparator()method.
 CheckboxMenuItem: It differs from MenuItem in that
it appears along with a checkbox. The selection can be
done with checkbox selected.
 To Enable or Disable a menu item,setEnabled()
method is true
STEPS OF CREATING JAVA AWTMENU
 Create menu bar: MenuBar mbobj=new
MenuBar();

 Add menu bar to the frame:setMenuBar(mbobj);

 Create menus:Menu mobj=new Menu(String str);

 Add menus to menu bar:mbobj.add(mobj);

 Create menu items:MenuItem miobj=new


MenuItem(String str);

 Add menu items to menus:mobj.add(miobj);


• Menu fileMenu = new Menu("File");
• package LayoutExamples;
• MenuItem openItem = new
• MenuItem("Open");
• MenuItem saveItem = new
• import java.awt.Frame; MenuItem("Save");
• import java.awt.Menu; • fileMenu.add(openItem);
• import java.awt.MenuBar; • fileMenu.add(saveItem);
• import java.awt.MenuItem; • fileMenu.addSeparator();
• menuBar.add(fileMenu);
• public class Menu1
• Menu editMenu=new Menu("Edit");
• {
• MenuItem cut=new
• public static void main(String[] args) { MenuItem("Cut");
• Frame frame = new Frame("Menu • MenuItem paste=new
Example"); MenuItem("Paste");
• MenuBar menuBar = new MenuBar(); • editMenu.add(cut);
• frame.setMenuBar(menuBar); • editMenu.add(paste);
• // Create a "File" menu • menuBar.add(editMenu);
• frame.setSize(300, 300);
• frame.setVisible(true);
• }
• }
• import java.awt.CheckboxMenuItem; • frame.setMenuBar(menuBar);
• import java.awt.Frame; • Menu m1 = new Menu("Insert");
• import java.awt.Menu; • Menu m2 = new Menu("Home");
• import java.awt.MenuBar; • menuBar.add(m1);
• • menuBar.add(m2);


• CheckboxMenuItem i1 = new
• public class Checkablemenu CheckboxMenuItem("Picture",true);
• { • CheckboxMenuItem i2 = new
• public static void main(String[] args) CheckboxMenuItem("Paste",false);
• { • m1.add(i1);
• Frame frame = new Frame("Menu • m2.add(i2);
Example"); •
• MenuBar menuBar = new MenuBar();
• • frame.setSize(500,300);
• frame.setVisible(true);

• }
DIALOGBOX
 Dialog boxes are pop-up windows on the screen
that appear for a small time to take either input
or display output while a main application is
running.
 A top-level pop-up window with a title and a
border that typically takes some form of input
from the user.
 It is used to display message and get specific
• information from the user.
 It is used to obtain user Input.
TYPES OF DIALOG BOXES
 Modal dialog box does not allow the user todo any
activity without dismissing (closing) it; example is File
Deletion Confirmation dialog box
 Modeless dialog box permits the user to do any
activity without closing it; example is Find and
Replace dialog box of MS Word. Java supports both
styles of dialog boxes.
CONSTRUCTOR

 Dialog(Frame parent)
 Dialog(Frame parent,Boolean mode)

 Dialog(Frame parent,String title)

 Dialog(Frame parent,String title,Boolean mode)


• package LayoutExamples;

• ok.addActionListener ( new ActionListener()
• import java.awt.Button; • {
• import java.awt.Dialog; • public void actionPerformed( ActionEvent e )
• import java.awt.FlowLayout; • {
• import java.awt.Frame; • // Hide dialog
• import java.awt.Label; • Dialogexample.d.setVisible(false);
• import java.awt.event.ActionEvent; • }
• import java.awt.event.ActionListener; • });
• •

• public class Dialogexample • d.add( new Label ("Click OK to continue"));


• • d.add( ok );

• {
• private static Dialog d; • // Show dialog
• • d.pack();
• d.setVisible(true);
• public static void main(String args[]) • System.exit(0);
• { • }
• Frame window = new Frame(); •


• // Create a modal dialog
• d = new Dialog(window, "Alert", true); • }
• •

• // Use a flow layout •


• d.setLayout( new FlowLayout() );

• // Create an OK button
• Button ok = new Button ("OK");
FILEDIALOG
 The FileDialog class displays a dialog window
from which the user can select a file.
 To create a file dialog box ,object of type
FileDialog is instatiated,this display std file
dialog box
CONSTRUCTOR
 FileDialog(Frame parent)
 FileDialog(Frame parent,String title)
FileDialog(Frame parent,String title,int how) whereas
how is
1) FileDialog.SAVE
This constant value indicates that the purpose of the
file dialog window is to locate a file to which to write.
2) FileDialog.LOAD
This constant value indicates that the purpose of the
file dialog window is to locate a file from which to read.
METHODS
 String getDirectory()Gets the directory of this file dialog.
 String getFile() Gets the selected file of this file
dialog.
 int getMode()Indicates whether this file dialog box
is for loading from a file or for saving to a file.

 Void setDirectory(String str)Sets the directory of this file


dialog window to be the specified directory.
 void setFile(String str)Sets the selected file for this file
dialog window to be the specified file.
 void setMode(int how)Sets the mode of the file
dialog.

You might also like