Awt Control
Awt Control
Types of Control
ScrollBar,TextField,TextArea.
UnderstandingLayout Managers
Menu Bars and Menus
Dialog Boxes
File Dialog
ScrollBars
Scroll bars are used to select continuous values
between a specified minimum and maximum.
Scroll bars may be oriented horizontally or
vertically.A scroll bar is actually a composite of
several individual parts.
Each end has an arrow that we can click to move
the current value of the scroll bar one unit in the
direction of the arrow.
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.
The slider box can be dragged by the user to a new
position. The scroll bar will then reflect this value.
Constructors:
Scrollbar( ):-creates a vertical scroll bar.
Scrollbar(int style):-allow us to specify the
orientation of the scroll bar. If style is
Scrollbar.VERTICAL, a vertical scroll bar is created.
If style is, Scrollbar.HORIZONTAL the scroll bar is
horizontal
Scrollbar(int style, int iValue, int tSize, int min, int
max):- the initial value of the scroll bar is passed in
iValue. The number of units represented by the height
of the thumb is passed in tSize. The minimum and
maximum values for the scroll bar are specified by
min and max.
METHODS:-
GETVALUE( ):-TO OBTAIN THE CURRENT VALUE OF THE SCROLL BAR.
SETVALUE( ):- IT THE CURRENT SETTING. FOR SETTING THE CURRENT
VALUE.
THESE METHODS ARE AS FOLLOWS:
INT GETVALUE( )
VOID SETVALUE(INT NEWVALUE)
WE CAN ALSO RETRIEVE THE MINIMUM AND MAXIMUM VALUES VIA
GETMINIMUM( ) AND GETMAXIMUM( ), SHOWN HERE:
INT GETMINIMUM( )
INT GETMAXIMUM( )
SETUNITINCREMENT( ):- BY DEFAULT, PAGE-UP AND PAGE-DOWN
INCREMENTS ARE 10.
SETBLOCKINCREMENT( ):- YOU CAN CHANGE THIS VALUE
THESE METHODS ARE SHOWN HERE:
VOID SETUNITINCREMENT(INT NEWINCR)
VOID SETBLOCKINCREMENT(INT NEWINCR)
EXAMPLE:-
IMPORT JAVA.AWT.*;
IMPORT JAVA.APPLET.*;
PUBLIC CLASS SBDEMO EXTENDS APPLET
{
STRING MSG = "";
SCROLLBAR VERTSB, HORZSB;
PUBLIC VOID INIT()
{
INT WIDTH = INTEGER.PARSEINT(GETPARAMETER("WIDTH"));
INT HEIGHT = INTEGER.PARSEINT(GETPARAMETER("HEIGHT"));
VERTSB = NEW SCROLLBAR(SCROLLBAR.VERTICAL,
0, 1, 0, HEIGHT);
HORZSB = NEW SCROLLBAR(SCROLLBAR.HORIZONTAL,
0, 1, 0, WIDTH);
ADD(VERTSB); ADD(HORZSB);
}
PUBLIC VOID PAINT(GRAPHICS G)
{
MSG = "VERTICAL: " + VERTSB.GETVALUE();
MSG += ", HORIZONTAL: " + HORZSB.GETVALUE();
G.DRAWSTRING(MSG, 6, 160);
}
}
TEXT FIELDS
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 control.
Constuctor:-
TextField( ):-creates a default text field.
USING INSETS
å GRID LAYOUT
FLOW LAYOUT
õ FlowLayout is the default layout
manager.
õ Implements similar to how words flow
in a text editor.
õ Components are laid out from the
upper-left corner, left to right and top to
bottom.
õ Fits to line. If not, appears in next line.
Insets getInsets( )
When overriding one of these methods, you must return a
new Insets object that contains the inset spacing you desire.
EXAMPLE :
// Demonstrate BorderLayout with Insets
import java.awt.*;
import java.applet.*;
import java.util.*;
public class BorderLayoutDemo extends Applet {
public void init()
{
setBackground(Color.Cyen);
setLayout(new BorderLayout());
add(new Button("This is across the top."), BorderLayout.NORTH);
add(new Label("The footer message might go here."),
BorderLayout.SOUTH);
add(new Button("Right"), BorderLayout.EAST);
add(new Button("Left"), BorderLayout.WEST);
String msg = "The Reasonable man adapts" + "himself to the world; \n" +
"the reasonable one persists in " + "trying to adapt the world to himself.
\n" +
"therefore all progress depends \n \n“;
add(new TextArea(msg), BorderLayout.CENTER);
}
public Insets getInsets() {
return new Insets(10,10,10,10);
}
}
VERSUS
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:
GridLayout( ) – Creates a single-column grid layout.
GridLayout(int numRows, int numColumns) –
Creates a grid layout with the specified number of
rows and columns.
GridLayout(int numRows, int numColumns, int
horz, int vert) – Allows you to specify the horizontal
& vertical space left b/w components.
Specifying numRows as Zero allows for unlimited-
length columns and viceversa for numColumns as Zero.
SAMPLE PROGRAM
// Demonstrate GridLayout
Example
MENU BARS AND MENUS
A top-level window can have a menu bar associated with it. A
menu bar displays a list of top-level menu choices. Each choice
is associated with a drop-down menu.
This concept is implemented in Java by the following classes: