Java AWT Choice Examples
|
 |
Usage:
The Choice class can be used to add a drop-down list of items to
a container.
Package:
Java.awt
Subclass
of:
Common
Constructors:
- Choice ()
- Creates an empty Choice
instance.
Common
Methods:
- void
addItemListener (ItemListener Handler)
- Configures an event handler for the
choice box.
- void insert
(String Item, int Index)
- Inserts the item into this choice at
the specified position.
- int
getSelectedIndex ()
- Returns the index of the selected
item.
- void
setBackground (Color BackgroundColor)
- Sets the background color of the
choice box.
- void setFont
(Font TextFont)
- Sets the font for this
component.
- void
setForeground (Color TextColor)
- Sets the color of the text for the
choice box.
- void Select
(int Selection)
- Sets the index of the selected
item.
Arguments:
-
BackgroundColor
- The color to be used for the background of the choice box.
-
Index
- Position where the item will be inserted.
-
Item
- A String to be listed in the choice box.
-
Handler
- The object which handles action events from this choice
box.
-
Selection
- The index of the selected item.
-
TextColor
- The color to be used for the text of the choice box.
-
TextFont
- The font to be used for the text of the choice box.
Example:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Choice2 extends Applet implements ItemListener {
/* Declaration */
private LayoutManager Layout;
private Choice Selector;
private Font SansSerif;
public Choice2 () {
/* Declaration */
String [] ColorList;
int i;
/* Instantiation */
ColorList = new String [9];
SansSerif = new Font ("SansSerif", Font.BOLD, 14);
Layout = new FlowLayout ();
Selector = new Choice ();
/* Decoration */
ColorList [0] = "Red";
ColorList [1] = "Magenta";
ColorList [2] = "Blue";
ColorList [3] = "Cyan";
ColorList [4] = "Green";
ColorList [5] = "Yellow";
ColorList [6] = "White";
ColorList [7] = "Gray";
ColorList [8] = "Black";
for (i = 0; i < ColorList.length; ++i) {
Selector.insert (ColorList [i], i);
}
Selector.setBackground (Color.yellow);
Selector.setForeground (Color.red);
Selector.setFont (SansSerif);
/* Location */
setLayout (Layout);
add (Selector);
/* Configuration */
Selector.addItemListener (this);
/* Initialization */
Selector.select (5);
setBackground (Color.yellow);
}
public void itemStateChanged(ItemEvent e) {
int Selection;
Selection = Selector.getSelectedIndex();
if (Selection == 0) {
setBackground (Color.red);
} else if (Selection == 1) {
setBackground (Color.magenta);
} else if (Selection == 2) {
setBackground (Color.blue);
} else if (Selection == 3) {
setBackground (Color.cyan);
} else if (Selection == 4) {
setBackground (Color.green);
} else if (Selection == 5) {
setBackground (Color.yellow);
} else if (Selection == 6) {
setBackground (Color.white);
} else if (Selection == 7) {
setBackground (Color.gray);
} else if (Selection == 8) {
setBackground (Color.black);
}
}
}
____________________________
Class java.awt.Choice
java.lang.Object
java.awt.Component
java.awt.Choice
public class Choice
implements ItemSelectable, Accessible
extends Component
The Choice class presents a pop-up menu of choices.
The current choice is displayed as the title of the menu.
The following code example produces a pop-up menu:
Choice ColorChooser = new Choice();
ColorChooser.add("Green");
ColorChooser.add("Red");
ColorChooser.add("Blue");
After this choice menu has been added to a panel,
it appears as follows in its normal state:
In the picture, "Green" is the current choice.
Pushing the mouse button down on the object causes a menu to
appear with the current choice highlighted.
Some native platforms do not support arbitrary resizing of Choice
components and the behavior of setSize()/getSize() is bound by
such limitations.
Native GUI Choice components' size are often bound by such
attributes as font size and length of items contained within
the Choice.
Constructor Index
| Constructor |
Description |
| Choice() |
Creates a new choice menu. |
Method Index
| Method |
Description |
| void add(String) |
Adds an item to this Choice menu. |
| void addItem(String) |
Adds an item to this Choice. |
| void addItemListener(ItemListener) |
Adds the specified item listener to receive item events from
this Choice menu. |
| void addNotify() |
Creates the Choice's peer. |
| int countItems() |
Deprecated.
As of JDK version 1.1,
replaced by getItemCount().
|
| AccessibleContext getAccessibleContext() |
Get the AccessibleContext associated with this Choice. |
| String getItem(int) |
Gets the string at the specified index in this
Choice menu. |
| int getItemCount() |
Returns the number of items in this Choice menu. |
| EventListener[] getListeners(Class) |
Return an array of all the listeners that were added to the Choice
with addXXXListener(), where XXX is the name of the listenerType
argument. |
| int getSelectedIndex() |
Returns the index of the currently selected item. |
| String getSelectedItem() |
Gets a representation of the current choice as a string. |
| Object[] getSelectedObjects() |
Returns an array (length 1) containing the currently selected
item. |
| void insert(String, int) |
Inserts the item into this choice at the specified position. |
| String paramString() |
Returns the parameter string representing the state of this
choice menu. |
| void processEvent(AWTEvent) |
Processes events on this choice. |
| void processItemEvent(ItemEvent) |
Processes item events occurring on this Choice
menu by dispatching them to any registered
ItemListener objects. |
| void remove(int) |
Removes an item from the choice menu
at the specified position. |
| void remove(String) |
Remove the first occurrence of item
from the Choice menu. |
| void removeAll() |
Removes all items from the choice menu. |
| void removeItemListener(ItemListener) |
Removes the specified item listener so that it no longer receives
item events from this Choice menu. |
| void select(int) |
Sets the selected item in this Choice menu to be the
item at the specified position. |
| void select(String) |
Sets the selected item in this Choice menu
to be the item whose name is equal to the specified string. |
Constructors
public Choice()
Creates a new choice menu. The menu initially has no items in it.
By default, the first item added to the choice menu becomes the
selected item, until a different selection is made by the user
by calling one of the select methods.
Methods
public void add(String item)
Adds an item to this Choice menu.
- Parameters
- item - the item to be added
- Throws
public void addItem(String item)
Adds an item to this Choice.
- Parameters
- item - the item to be added
- Throws
public synchronized void addItemListener(ItemListener l)
Adds the specified item listener to receive item events from
this Choice menu.
If l is null, no exception is thrown and no action is performed.
public void addNotify()
Creates the Choice's peer. This peer allows us to change the look
of the Choice without changing its functionality.
Deprecated.
As of JDK version 1.1,
replaced by getItemCount().
public int countItems()
public AccessibleContext getAccessibleContext()
Get the AccessibleContext associated with this Choice.
Creates a new context if necessary.
- Returns
- the AccessibleContext of this Choice
- Overrides
public String getItem(int index)
Gets the string at the specified index in this
Choice menu.
- Parameters
- index - the index at which to begin.
public int getItemCount()
Returns the number of items in this Choice menu.
public EventListener[] getListeners(Class listenerType)
Return an array of all the listeners that were added to the Choice
with addXXXListener(), where XXX is the name of the listenerType
argument. For example, to get all of the ItemListener(s) for the
given Choice c, one would write:
ItemListener[] ils = (ItemListener[])(c.getListeners(ItemListener.class))
If no such listener list exists, then an empty array is returned.
- Parameters
- listenerType - Type of listeners requested
- Returns
- all of the listeners of the specified type supported by this choice
- Overrides
public int getSelectedIndex()
Returns the index of the currently selected item.
public synchronized String getSelectedItem()
Gets a representation of the current choice as a string.
- Returns
- a string representation of the currently
selected item in this choice menu.
public synchronized Object[] getSelectedObjects()
Returns an array (length 1) containing the currently selected
item. If this choice has no items, returns null.
public void insert(String item,
int index)
Inserts the item into this choice at the specified position.
- Parameters
- item - the item to be inserted
- index - the position at which the item should be inserted
- Throws
protected String paramString()
Returns the parameter string representing the state of this
choice menu. This string is useful for debugging.
- Returns
- the parameter string of this
Choice menu.
- Overrides
protected void processEvent(AWTEvent e)
Processes events on this choice. If the event is an
instance of ItemEvent, it invokes the
processItemEvent method. Otherwise, it calls its
superclass's processEvent method.
protected void processItemEvent(ItemEvent e)
Processes item events occurring on this Choice
menu by dispatching them to any registered
ItemListener objects.
This method is not called unless item events are
enabled for this component. Item events are enabled
when one of the following occurs:
- An
ItemListener object is registered
via addItemListener.
- Item events are enabled via
enableEvents.
public void remove(int position)
Removes an item from the choice menu
at the specified position.
- Parameters
- position - the position of the item.
public void remove(String item)
Remove the first occurrence of item
from the Choice menu.
- Parameters
- item - the item to remove from this
Choice menu.
- Throws
public void removeAll()
Removes all items from the choice menu.
public synchronized void removeItemListener(ItemListener l)
Removes the specified item listener so that it no longer receives
item events from this Choice menu.
If l is null, no exception is thrown and no action is performed.
public synchronized void select(int pos)
Sets the selected item in this Choice menu to be the
item at the specified position.
- Parameters
- pos - the positon of the selected item.
- Throws
public synchronized void select(String str)
Sets the selected item in this Choice menu
to be the item whose name is equal to the specified string.
If more than one item matches (is equal to) the specified string,
the one with the smallest index is selected.
- Parameters
- str - the specified string