AWT controls
AWT Controls and event listeners
AWT Controls and event listeners
Label
A label is an object of type Label, and it contains a string, which it displays. Labels are passive controls that do not support any interaction with the user.
Label defines the following constructors:
Label( )
Label(String str)
Label(String str, int how)
The value of how
Label.LEFT,
Label.RIGHT, or
Label.CENTER.
Label Methods
•
setText()
•
getText()
•
void setAlignment(int how)
•
int getAlignment( )
Buttons
•
A
push button is a component that
contains a label
and that generates an event when it is pressed.
Push buttons are objects of type
Button.
Button defines these two constructors:
•
void setLabel(String str)
•
String getLabel( )
Problem
Design a window/applet in which there are
three buttons and whenever anyone of
them is clicked a message is generated
informing which button is clicked
public class ButtonDemo extends Applet implements ActionListener {
String msg = "";
Button yes, no, maybe;
public void init() {
yes = new Button("Yes"); no = new Button("No");
maybe = new Button("Undecided"); add(yes); add(no); add(maybe); yes.addActionListener(this); no.addActionListener(this); maybe.addActionListener(this); } public void
actionPerformed(ActionEvent ae) {
String str = ae.getActionCommand(); if(str.equals("Yes")) {
msg = "You pressed Yes."; }
else if(str.equals("No")) { msg = "You pressed No.";
} else {
msg = "You pressed Undecided."; }
repaint(); }
public void paint(Graphics g) {
g.drawString(msg, 6, 100);
} }
TextField
• 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.
constructors:
• TextField( )
• TextField(int numChars)
• TextField(String str)
• TextField(String str, int numChars)
TextField Methods
•
String getText( )
•
void setText(String str)
•
String getSelectedText( )
•
void select(int startIndex, int endIndex)
•
void setEditable(boolean canEdit)
•
boolean isEditable( )
•
void setEchoChar(char ch)
•
boolean echoCharIsSet( )//is it in echo mode
•
char getEchoChar( )
problem
Design a login form where username and pwd
must be same for a successful login.
Check Boxes
A
check box is a control that is used to turn an option
on or off
Checkbox supports these 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 for checkboxes
•
boolean getState( )
•
void setState(boolean on)
•
String getLabel( )
•
void setLabel(String str)
Event handling
•
Ckbox.addItemListener(this)//register listener
•
public void itemStateChanged(ItemEvent ie)
//handler method
Problem
Design a window like this either using an
applet or a Frame and record the event
when ever a change occurs in the state of
the checkbox.
public class CheckboxDemo extends Applet implements ItemListener {
String msg = "";
Checkbox Car, Bike, Laptop, Guitar; public void init() {
Car = new Checkbox("Car", null, true); Bike = new Checkbox("Bike");
Laptop = new Checkbox("Laptop"); Guitar = new Checkbox("Guitar"); add(Car); add(Bike); add(Laptop); add(Guitar); Car.addItemListener(this); Bike.addItemListener(this); Laptop.addItemListener(this); Guitar.addItemListener(this); } 15
public void itemStateChanged(ItemEvent ie) { repaint();
}
// Display current state of the check boxes. public void paint(Graphics g) {
msg = "Current state: "; g.drawString(msg, 6, 80);
msg = " Car: " + Car.getState(); g.drawString(msg, 6, 100);
msg = " Bike: " + Bike.getState(); g.drawString(msg, 6, 120);
msg = " Laptop: " + Laptop.getState(); g.drawString(msg, 6, 140);
msg = " Guitar: " + Guitar.getState(); g.drawString(msg, 6, 160);
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
•
How to create
•
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
Which radio button is selected?
•
Call following methods on checkbox group object
•
Checkbox getSelectedCheckbox( )
•
void setSelectedCheckbox(Checkbox which)
Event handling
•
Ckbox.addItemListener(this)//register listener
•
public void itemStateChanged(ItemEvent ie)
//handler method
Problem
Design a window like this and record
which option is selected whenever a
state change occurs in the group
public class CheckboxGroupDemo extends Applet implements ItemListener
{
String msg = "";
Checkbox Car, Bike, Laptop, Guitar;
CheckboxGroup cbg;
public void init() {
cbg=new CheckboxGroup();
Car = new Checkbox("Car", cbg, true); Bike = new Checkbox("Bike",cbg,false);
Laptop = new Checkbox("Laptop",cbg,false); Guitar = new Checkbox("Guitar",cbg,false); add(Car); add(Bike); add(Laptop); add(Guitar); Car.addItemListener(this); Bike.addItemListener(this); Laptop.addItemListener(this); Guitar.addItemListener(this); } 19
public void itemStateChanged(ItemEvent ie) {
repaint(); }
public void paint(Graphics g) {
msg = "Current Selection "; g.drawString(msg, 6, 80);
msg=cbg.getSelectedCheckbox().getLabel();
g.drawString(msg, 6, 100); }
Choice
•
The Choice class is used to create a
pop-up list of
items from which the user may
choose.
•
When inactive, a Choice component takes up only
enough space to show the currently selected item.
•
When the user clicks on it, the whole list of choices
pops up, and a new selection can be made
•
Choice only defines the default constructor, which
creates an empty list.
To add a selection to the list
•
void addItem(String name)
•
void add(String name)
Choice methods
•
String getSelectedItem( )
•
int getSelectedIndex( )
•
int getItemCount( )
•
void select(int index)
•
void select(String name)
•
String getItem(int index)
Problem
Design a window/applet in which u ask
the user to select his/her branch and
Sem.
public class ChoiceDemo extends Applet implements ItemListener { Choice branch, sem;
String msg = ""; public void init() {
branch = new Choice(); sem = new Choice();
// add items to branch list branch.add("CSE");
branch.add("IT"); branch.add("ECE"); branch.add("ME");
// add items to sem list sem.add("1");
sem.add("3"); sem.add("5"); sem.add("7");
// add choice lists to window add(branch);
add(sem);
// register to receive item events branch.addItemListener(this); sem.addItemListener(this); }
23 public void
itemStateChanged(ItemEvent ie) {
repaint(); }
public void paint(Graphics g) { msg = "Current Branch ";
msg
+=
branch.getSelectedItem(); g.drawString(msg, 6, 80);msg = "Current Sem ";
msg += sem.getSelectedItem(); g.drawString(msg, 6, 100);
Lists
• The List class provides a compact, multiple-choice, scrolling selection list.
• It can be used to show any number of choices in the visible window.
• It can also used for multiple selections.
List provides these constructors:
List( )
List(int numRows)// no of visible entries List(int numRows, boolean multipleSelect)
Adding items to list
• void add(String name)
• void add(String name, int index)
List methods
For single selection
• String getSelectedItem( ) • int getSelectedIndex( )
for multiple selection
• String[ ] getSelectedItems( ) • int[ ] getSelectedIndexes( )
• int getItemCount( ) • void select(int index)
25
Sachin Tendulkar Yuvi
Scroll bars
• Scroll bars are used to select continuous values between a specified minimum and maximum.
• Scroll bars may be oriented horizontally or vertically.
Constructors
• Scrollbar( ) //Scrollbar.VERTICAL
• Scrollbar(int style) //Scrollbar.VERTICAL | HORIZONTAL • Scrollbar(int style, int initialValue, int thumbSize, int
min, int max)
Scrollbar methods
• void setValues(int initialValue, int thumbSize, int min, int max)
• int getValue( )
• void setValue(int newValue)
• int getMinimum( )
• int getMaximum( )
• void setUnitIncrement(int newIncr)//1
• void setBlockIncrement(int newIncr)//10 page up
ScrollBar Event handling
Listener
AdjustmentListener
Method
Public void adjustmentValueChanged(AdjustmentEvent ae)
{
}
Problem
Create an applet in which we have three scollbars
each one for the R G and B component of color. On
the basis of the values adjusted by these scrollbar
change the Background Color of applet
public class SBColor extends Applet implements AdjustmentListener {
int r,g,b;
Scrollbar redSB, greenSB, blueSB;
public void init() {
/orientation,initVal,ThumbSize,min,max
redSB = new Scrollbar(Scrollbar.HORIZONTAL,0, 5, 0, 255); greenSB = new Scrollbar(Scrollbar.HORIZONTAL,0, 5, 0, 255); blueSB = new Scrollbar(Scrollbar.HORIZONTAL,0, 5, 0, 255); add(redSB); add(greenSB); add(blueSB); redSB.addAdjustmentListener(this); greenSB.addAdjustmentListener(this); blueSB.addAdjustmentListener(this); }
public void adjustmentValueChanged(AdjustmentEvent ae) { r=redSB.getValue(); g=greenSB.getValue(); b=blueSB.getValue(); repaint(); } 30
public void paint(Graphics gg)
{ setBackground(new Color(r,g,b));
TextArea
• Multiline text FieldFollowing are the constructors for 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)
numLines specifies the height, in lines, of the text area, and
numChars specifies its width, in characters.
sBars must be one of these values:
– SCROLLBARS_BOTH – SCROLLBARS_NONE
– SCROLLBARS_HORIZONTAL_ONLY – SCROLLBARS_VERTICAL_ONLY
Methods
•
getText( ),
•
setText( ),
•
getSelectedText( ),
•
select( ),
•
isEditable( )
•
setEditable( )
•
void append(String str)
•
void insert(String str, int index)
•
void replaceRange(String str, int startIndex, int
endIndex)
Menu
33
(Swings)
JMenuBar
JMenu menu, submenu;
JMenuItem
Parts of menu System
34
MenuBar
Menu
Menu Items
Sub Menu
Add a MenuBar to the Frame
MenuBar mbar=new MenuBar()
setMenuBar(mbar)
Add menu to menu bar
•
Add(menu)
Menu
• A Menu object is a pull-down menu component that is deployed from a
menu bar.
• A menu can optionally be a tear-off menu. A tear-off menu can be
opened and dragged away from its parent menu bar or menu. It remains on the screen after the mouse button has been released.
Menu constructors
• Menu( )
• Menu(String optionName)
• Menu(String optionName, boolean removable)
Imp methods
• add(MenuItem mi) Adds the specified menu item to this menu.
• add(String label) Adds an item with the specified label to this menu.
• addSeparator() Adds a separator line, or a hypen, to the menu at the current position.
Menu Item
Constructor Summary
• MenuItem ()
Constructs a new MenuItem with an empty label and no keyboard shortcut. • MenuItem(String label)
Constructs a new MenuItem with the specified label and no keyboard shortcut. • MenuItem (String label, Menu Shortcut s)
Create a menu item with an associated keyboard shortcut.
Checkbox MenuItem Constructor Summary
• CheckboxMenuItem()
Create a check box menu item with an empty label. • CheckboxMenuItem (String label)
Create a check box menu item with the specified label. • CheckboxMenuItem (String label, boolean state)
Create a check box menu item with the specified label and state.
Menu and menu items
• Individual menu items are of type MenuItem.
It defines these constructors:
• MenuItem( )
• MenuItem(String itemName)
• MenuItem(String itemName, MenuShortcut keyAccel)
Methods
• setEnable(boolean)
• isEnable()
• void setLabel(String newName)
• String getLabel( )
Problem
Design a frame like this in which,
which ever color the user selects from
the Background menu it becomes the
background color of the frame
class MenuTest extends Frame implements ActionListener {MenuBar mbar; MenuTest() { setTitle("MenuTest"); mbar=new MenuBar(); setMenuBar(mbar); Background=new Menu("Background"); MenuItem r,g,b; r=new MenuItem("Red"); g=new MenuItem("Green"); b=new MenuItem("Blue"); Background.add(r); Background.add(g); Background.add(b); mbar.add(Background); r.addActionListener(this); About=new Menu("About"); mbar.add(About); setSize(200,200); setVisible(true); } 40 public void actionPerformed(ActionEvent ae) { if (ae.getActionCommand().equals("Red")) { setBackground(Color.red); repaint();} }
public static void main(String args[]) {
new MenuTest(); }