The java.awt.
Choice component implements a list of items where only the selected item is
displayed. The list appears as a drop down (pop-down menu) menu can be seen through the
touch of a button built into the component, so it is also known as a check box or combobox. In
the same manner as in component java.awt.List a vertical scrollbar is automatically displayed
when the list can not simultaneously show all the items they contain. The selection only
operate in simple mode, that is, only one item can be selected at a time, and the choice of an
item not selected selects and vice versa.
Choice defines only the default constructor which creates an empty list.
This component generates only type events ItemEvent, thus requiring the implement the
ItemListener interface for processing
Method Description
Choice () Constructs a checkbox.
add (String) Adds the specified text as a new item the end of the
list.
addItemListener (ItemEvent) Registers a listener class (processor events)
ItemListener for the component.
getItem (int) Gets the indicated item.
GetItemCount () Gets the number of items in the list.
getSelectedIndex () Gets the position of the selected item.
GetSelectedItem () Gets the selected item.
insert (String, int) Inserts the given item at the specified position.
remove (int) Removes the specified item from the list.
removeAll () Removes all items from the list.
select (INT) Selects the item displayed in the list.
import java.applet.Applet;
import java.awt.Choice;
import java.awt.event.*;
import java.awt.*;
/*
<applet code="ChoiceExample" width=200 height=200>
</applet>
*/
public class ChoiceExample extends Applet implements ItemListener{
String m="";
Choice language;
public void init(){
language = new Choice();
language.add("Java");
language.add("C++");
language.add("VB");
language.add("Perl");
//add choice or combobox
add(language);
language.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
m="selected language is:";
m+=language.getSelectedItem();
g.drawString(m,100,120);
}
}
List
It provides compact, multiple choice, scrolling selection list.The list object can be constructed to
show any number of choices in the visible window.It can also be created to allow multiple
selections.
Constructors:
List() throws HeadlessException
List(int numrows) throws HeadlessException
List(int numrows,Boolean select) throws HeadlessException
numrows specifies the number of entries in the list that will always be visible. In the third form if
second parameter is true then user may select two or more items at a time. If false then only one
item may be selected.
To add a selection to the list we can use
void add(String n)
void add(String n,int i)
i specifies the index where the selection is added.
For the list that allow only single selection we can determine which item is currently selected by
using :
i) String getSelectedItem()
ii) int getSelectedIndex()
Program
import java.applet.*;
import java.awt.*;
/*
<applet code="ListExample" width=200 height=200>
</applet>
*/
public class ListExample extends Applet {
public void init(){
//this list will have 5 visible rows
List list = new List(5);
list.add("One");
list.add("Two");
list.add("Three");
list.add("Four");
list.add("Five");
list.add("Six");
list.add("Seven");
list.select(2);
add(list);
}
}
Example output:
Text Field: This class implements single line text entry area called as edit control. It allows
user to enter strings and to edit text using arrow keys cut & paste keys & mouse selections. It is the
sub class of Text Component.
Constructors:
TextField (): throws HeadlessException
TextField (int nchars): throws HeadlessException
TextField (String s): throws HeadlessException
TextField (String s, int nchars): throws HeadlessException
Second constructor creates a textfield that is nchar wide
Third constructor initializes the text field with the String s
To obtain the String currently present in the text field we can use
String getText()
To set the text we can use
Void setText(String s)
Program
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="TextFieldExample" width=400 height=800>
</applet>
*/
public class TextFieldExample extends Applet implements ActionListener
{
TextField t1;
public void init()
{
setForeground(Color.red);
t1=new TextField(20);
add(t1);
t1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
public void paint(Graphics g)
{
g.drawString("content is:" + t1.getText(), 10, 50);
g.drawString("selected text is:" + t1.getSelectedText(), 10, 150);
}
}
Text Area:
It is a multi line editor.
Constructors:
TextArea(): throws HeadlessException
TextArea(int nl,int nc): throws HeadlessException
TextArea(String s): throws HeadlessException
TextArea(String s, int nl,int nc): throws HeadlessException
TextArea(String s, int nl,int nc,int sbar): throws HeadlessException
Nl specifies the height in lines,nc specifies its width in characters,s is the initial textsbar is the
scroll bar which can be
SCROLLBARS_BOTH
SCROLLBARS_NONE
SCROLLBARS_HORIZONTAL_ONLY
SCROLLBARS_VERTICAL_ONLY
Program
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="TextAreaExample" width=400 height=800>
</applet>
*/
public class TextAreaExample extends Applet
{
public void init()
{
setForeground(Color.red);
String s="Hi how r u \n"+"i am fine\n";
TextArea t1=new TextArea(s,10,30);
add(t1);
}
}
Layout Managers: It automatically arranges your controls within a window. Each container has a
layout manager with it. The layout can be set by using set Layout (), if not mentioned the default
flow layout is used. Whenever a container is resized the layout manager is used to position each of
the components with it.
Flowlayout:It is the default layout manager which arranges components left to right, top to bottom.
So components are layed out line by line beginning at upper left corner. When a line is filled it
goes to next line