AWT (contd..)

Dated : 04/23/2001

 

Control Fundamentals

The AWT supports following types of controls:

Labels

Push Buttons

Check Boxes

Choice Lists

Lists

Scroll Bars

Text Editing

These controls are subclass of Component.

Adding and Removing Controls

Use add() method which is defined by Container class. // add() has several forms.

Component add (Component compObj)

A reference of compObj is returned .

Once a control has been added, it is automatically visible whenever its parent window is displayed.

void remove (Component compObj)

To remove all controls by calling removeAll() method.

Responding to Controls

Except for labels, which are apssive controls, all controls generate events when they are accessed by the user.

Labels

Constructors:

Label ()

Label (String str)

Label (String str, int how)

The value of how must be one of these three constants:

Label.LEFT, Label.RIGHT, or Label.CENTER.

Methods

void setText (String str)

String getText ()

void setAlignment (int how)

int getAligment ()

Buttons

A push button is a component that contains a label and that generates an event when it is pressed.

Objects of type Button.

Button()

Button (String str)

Methods

void setLabel (String str)

String getLabel ()

For handling events, implement the ActionListener interface.

Sample Code

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="ButtonDemo" width=300 height=200>

</applet>

*/

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, 10, 100);

}

}

In addition to comparing button labels, you can also determine which button has been pressed, by comparing the object obtained from the getSource () method.

Piece of Code

Button yes = new Button ("Yes");

bList [0] = (Button) add (yes); // bList is array of Button types

public void actionPerformed (ActionEvent ae) {

if (ae.getSource() == bList [0]) {

msg = "You Pressed " + bList[0].getLabel ();

}

}

 

CheckBox

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)

CheckboxGroup

It is possible to create a set of mutually exclusive cj=heckboxes in which one and only one check box in the group can be checked at any time.

Often called radio buttons.

Methods

Checkbox getSelectedCheckbox()

void setSelectedCheckBox (Checkbox which)

 

Sample Code

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="CBGroup" width=300 height=200>

</applet>

*/

public class CBGroup extends Applet implements ItemListener {

String msg = "";

Checkbox win98, winNT, solaris, mac;

CheckboxGroup cbg;

public void init () {

cbg = new CheckboxGroup ();

win98 = new Checkbox ("Windows 98", cbg, true);

winNT = new Checkbox ("Windows NT", cbg, false);

solaris = new Checkbox ("Solaris", cbg, false);

mac = new Checkbox ("MacOS", cbg, false);

add (win98);

add (winNT);

add(solaris);

add(mac);

win98.addItemListener (this);

winNT.addItemListener (this);

solaris.addItemListener (this);

mac.addItemListener (this);

}

public void itemStateChanged(ItemEvent ie) {

repaint ();

}

public void paint (Graphics g) {

msg = "Current Selection: ";

msg += cbg.getSelectedCheckbox().getLabel();

g.drawString (msg, 10, 100);

}

}

Choice Controls

The Choice class is used to create a pop-up list from which the user may choose.

Only defines a default constructor, which creates an empty list.

Methods

void addItem (String name)

void add (String name)

String getSelectedItem ()

int getSelectedIndex ()

int getItemCount ()

void select (int index)

void select (String name)

String getItem (int index)

Lists

The class List provides a compact, multiple- choice selection list.

Unlike Choice object, which shows only the single selected item in the menu, a List can be constructed to show any number of choices in the visible window. It can alsobe created to allow multiple selsctions.

Constructors:

List ()

List (int numRows)

List (int numRows, Boolean multipleSelect) // numRows is rows always visible

Methods

void add (String name)

void add (String name, int index)

String getSelectedItem ()

int getSelectedIndex ()

String[] getSelectedItems ()

int[] getSelectedIndexes ()

int getItemCount ()

void select (int index)

String getItem (int index)

 

Sample Code

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

 

 

 

/*

<applet code="ListDemo" width=300 height=200>

</applet>

*/

public class ListDemo extends Applet implements ActionListener {

List os, browser;

String msg = "";

public void init () {

os = new List (4, true);

browser = new List (4, false);

os.add ("Windows 98");

os.add ("Windows NT");

os.add ("Solaris");

os.add ("MacOS");

browser.add ("Netscape 1.1");

browser.add ("Netscape 2.x");

browser.add ("Netscape 3.x");

browser.add ("Netscape 4.x");

browser.add ("Internet Explorer 2.0");

browser.add ("Internet Explorer 3.0");

browser.add ("Internet Explorer 4.0");

browser.add ("Lynx 2.4");

browser.select (1);

add (os);

add (browser);

os.addActionListener (this);

browser.addActionListener (this);

}

public void actionPerformed (ActionEvent ae) {

repaint ();

}

 

 

 

 

public void paint (Graphics g) {

int idx [];

msg = "Current OS: ";

idx = os.getSelectedIndexes ();

for (int i =0; i<idx.length; i++) {

msg += os.getItem (idx[i] ) + " ";

}

g.drawString (msg, 10, 100);

msg = "Current Browser: ";

msg += browser.getSelectedItem ();

g.drawString (msg, 10, 130);

}

}

Scroll Bars

Scroll bars are used to select continuous values between a specified minimum and maximum.

Constructors

Scrollbar ()

Scrollbar (int style)

Scrollbar (int style, int initialValue, int thumbSize, int min, int max)

Style is Scrollbar.HORIZONTAL or Scrollbar.VERTICAL.

Methods

void setValues (int initialValue, int thumbSize, int min, int max)

int getValue ()

void setValue (int newValue)

int getMaximum ()

int getMinimum ()

By Default, 1 is the increment added to or subtracted from the scroll bar each time it is scrolled up or down one line. By default, page-up and page-down increments are 10.

void setUnitIncrement (int newIncr)

void setBlockIncrement (int newIncr)

Handling Scroll bars

Implement AdjustmentListener interface. Each time a user interacts with the scroll bar, an AdjustmentEvent object is generated. Its getAdjustmentType () method can be used to determine the type of the adjustment.

 

 

 

The types of adjustment events are as follows:

BLOCK_DECREMENT

BLOCK_INCREMENT

TRACK

UNIT_DECREMENT

UNIT_INCREMENT

 

TextField

TextField class implements a single-line text-entry area, usually called an edit control.

TextField is a subclass of TextComponent.

Constructors:

TextFiled ()

TextFiled (int numChars)

TextFiled (String str)

TextFiled (String str, int numChars)

Methods

String getText ()

void setText (String str)

String getSelectedText ()

void select (int startIndex, int endIndex)

boolean isEditable ()

void setEditable (Boolean canEdit)

void setEchoChar (char ch)

boolean echoCharIsSet ()

char getEchoChar ()

TextArea

Constructors:

TextArea ()

TextArea (int numLines, int numChars) // height and width

TextArea (String str)

TextArea (String str, int numLines, int numChars)

TextArea (String str, int numLines, int numChars, int sBars)

Value of sbars must be one of the following:

SCROLLBARS_BOTH

SCROLLBARS_NONE

SCROLLBARS_HORIZONTAL_ONLY

SCROLLBARS_VERTICAL_ONLY

Methods

getText (), setText (), getSelectedText (), select (), isEditable (), and setEditable()

void append (String str)

void insert (String str, int index)

void replaceRange (String str, int startIndex,int endIndex) // replaces characters from

//startIndex to endIndex