Chapter 1
Chapter 1
Archana Gopnarayan
Lecturer , Department of Information Technology (NBA Accrediated)
Vidyalankar Polytechnic
AWT (Abstract Window Toolkit)
import java.awt.*;
Component
Container
Window Panel
Frame Applet
Construct:
void setText(String str)
String getText( )
Label
• We can set the alignment of the string within the label by
calling setAlignment( ).
• To obtain the current alignment, method is getAlignment( ).
Construct:
void setAlignment(int how)
int getAlignment( )
Label
import java.awt.*;
import java.applet.*;
/* <applet code="LabelDemo" width=300 height=200> </applet> */
public class LabelDemo extends Applet {
public void init() {
Label l1 = new Label("One");
Label l2 = new Label("Two");
Label l3 = new Label("Three");
// add labels to applet window
add(l1);
add(l2);
add(l3);
}
}
Label
import java.awt.*;
import java.applet.*;
/* <applet code="LabelDemo" width=300 height=200> </applet> */
Constructor:
Button( )
Button(String str)
Button
• We can set its label by calling setLabel( ).
• We can retrieve its label by calling getLabel( ).
Checkbox( )
Checkbox(String str)
boolean getState( )
String getLabel( )
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.
• These check boxes are often called radio buttons.
• To create a set of mutually exclusive check boxes, you must
first define the group to which they will belong and then
specify that group when you construct the check boxes.
• Check box groups are objects of type CheckboxGroup.
CheckboxGroup
• You can determine which check box in a group is currently
selected by calling getSelectedCheckbox( ).
• You can set a check box by calling setSelectedCheckbox( ).
Checkbox getSelectedCheckbox( )
Choice only defines the default constructor, which creates an empty list.
Choice()
• To add a selection to the list, call add( ).
add(String name)
Choice Controls
• To determine which item is currently selected ,getSelectedItem( ) or
getSelectedIndex( ).
String getSelectedItem( )
int getSelectedIndex( )
• The getSelectedItem( ) method returns a string containing the name of the
item.
• getSelectedIndex( ) returns the index of the item.
• The first item is at index 0. By default, the first item added to the list is
selected.
• To obtain the number of items in the list, call getItemCount( ).
• We can set the currently selected item using the select( ) method .
Choice Controls
int getItemCount( )
void select(int index)
void select(String name)
String[ ] getSelectedItems( )
int[ ] getSelectedIndexes( )
String getText( )
void setText(String str)
boolean isEditable( )
• We can disable the echoing of the characters as they are typed
by calling setEchoChar( ).
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)
TextArea
Constant values for Bars:
SCROLLBARS_BOTH
SCROLLBARS_NONE
SCROLLBARS_HORIZONTAL_ONLY
SCROLLBARS_VERTICAL_ONLY
Scrollbar
• Scroll bars are used to select continuous values between a specified
minimum and maximum.
• The current value of the scroll bar relative to its minimum and maximum
values is indicated by the slider box (or thumb) for the scroll bar.
Scrollbar
Scrollbar( )
Scrollbar(int style)
Scrollbar(int style, int initialValue, int thumbSize, int min, int max)
If you construct a scroll bar by using one of the first two constructors, then you need
to set its parameters by using setValues( )
int getValue( )
void setValue(int newValue)
int getMinimum( )
int getMaximum( )
Scrollbar
• By default, 1 is the increment added to or subtracted from the scroll bar
each time it is scrolled up or down one line.
• You can change this increment by calling setUnitIncrement( ).
• By default, page-up and page-down increments are 10. You can change this
value by calling setBlockIncrement( ).
}
}
Arranging components
• Every Container has a layout manager
• The default layout for a Panel is FlowLayout
• The default layout for a Applet is FlowLayout
• You could set it explicitly with
setLayout (new FlowLayout( ));
FlowLayout
• FlowLayout is the default layout .
• Components are laid out from the upper-left corner, left to right and top
to bottom.
constructors for FlowLayout:
FlowLayout( )
Ex: setLayout(new FlowLayout());
or
FlowLayout f=new FlowLayout();
setLayout(f);
FlowLayout(int how)
Ex: setLayout(new FlowLayout(FlowLayout.LEFT));
• The third form allows you to specify the horizontal and vertical space
left between components in horz and vert, respectively.
FlowLayout
import java.awt.*;
import java.applet.*;
/* <applet code="FlowLayoutDemo" width=250 height=200></applet>*/
public class FlowLayoutDemo extends Applet
{
Checkbox Win98, winNT, solaris, mac;
public void init()
{
setLayout(new FlowLayout(FlowLayout.RIGHT));
Win98 = new Checkbox("Windows 98/XP", null, true);
winNT = new Checkbox("Windows NT/2000");
solaris = new Checkbox("Solaris");
mac = new Checkbox("MacOS");
add(Win98);
add(winNT);
add(solaris);
add(mac);
}
BorderLayout
add(win98,BorderLayout.EAST);
add(winNT,BorderLayout.WEST);
add(solaris,BorderLayout.NORTH);
add(mac,BorderLayout.SOUTH);
add(win10,BorderLayout.CENTER);
}
GridLayout
• GridLayout lays out components in a two-dimensional grid. When you
instantiate a GridLayout, you define the number of rows and columns.
The constructors supported by GridLayout are shown here:
GridLayout( )
GridLayout(int numRows, int numColumns)
GridLayout(int numRows, int numColumns, int horz, int vert)
Example
import java.awt.*;
import java.applet.*;
/* <applet code="gridlayout" width=250 height=200></applet>*/
public class gridlayout extends Applet
{
After you have created a deck, your program activates a card by calling one of
the following methods defined by CardLayout:
Win.addActionListener(this);
Other.addActionListener(this);
}
MenuItem( )
MenuItem i1=new MenuItem();
MenuItem(String itemName)
MenuItem i1=new MenuItem("BLUE");
MenuItem(String itemName, MenuShortcut keyAccel)
CheckboxMenuItem( )
CheckboxMenuItem(String itemName)
CheckboxMenuItem(String itemName, boolean on)
setEnabled(): Disabled or enabled the item
void setEnabled(boolean value)
isEnabled(): Determine the status
Boolean isEnabled()
setLabel()
void setLabel(String name)
getLabel()
String getLabel()
setState()
void setState(Boolean checked)
getState()
Boolean getState()
Example
import java.awt.*;
public class menucolour extends Frame
{
menucolour()
{
setSize(500,500);
setVisible(true);
MenuBar mb=new MenuBar();
setMenuBar(mb);
Menu m1=new Menu("COLOR");
mb.add(m1);
MenuItem i1=new MenuItem("BLUE");
MenuItem i2=new MenuItem("RED");
MenuItem i3=new MenuItem("BLACK");
m1.add(i1);
m1.add(i2);
m1.add(i3);
Menu m2=new Menu("FONT");
mb.add(m2);
MenuItem f1=new MenuItem("12");
MenuItem f2=new MenuItem("14");
MenuItem f3=new MenuItem("16");
m2.add(f1);
m2.add(f2);
m2.add(f3);
}
public static void main(String args[])
{
menucolour mc=new menucolour();
}
}
Dialog Box
• Dialog boxes are primarily used to obtain user input and are often child
windows of a top-level window.
• Dialog boxes don’t have menu bars, but in other respects, they function like
frame windows.
• Dialog boxes may be modal or modeless.
• When a modal dialog box is active, all input is directed to it until it is
closed. This means that you cannot access other parts of your program until
you have closed the dialog box.
• When a modeless dialog box is active, input focus can be directed to
another window in your program. Thus, other parts of your program remain
active and accessible.
•Two commonly used constructors are shown here:
– Dialog(Frame parentWindow, boolean mode)
– Dialog(Frame parentWindow, String title, boolean mode)
•Here, parentWindow is the owner of the dialog box. If mode is true, the dialog
box is modal. Otherwise, it is modeless.
File Dialog
File dialog box is the standard file dialog box provided by the operating
system.
FileDialog constructors:
FileDialog(Frame parent)
FileDialog(Frame parent, String boxName)
FileDialog(Frame parent, String boxName, int how)
Here, parent is the owner of the dialog box. The boxName parameter
specifies the name displayed in the box’s title bar. If boxName is omitted,
the title of the dialog box is empty.
If how is FileDialog.LOAD, then the box is selecting a file for reading. If
how is FileDialog.SAVE, the box is selecting a file for writing.
If how is omitted, the box is selecting a file for reading.