Methods Summary |
---|
public void | add(java.lang.String item)Adds an item to this Choice menu.
addItem(item);
|
public void | addItem(java.lang.String item)Obsolete as of Java 2 platform v1.1. Please use the
add method instead.
Adds an item to this Choice menu.
synchronized (this) {
insertNoInvalidate(item, pItems.size());
}
// This could change the preferred size of the Component.
if (valid) {
invalidate();
}
|
public synchronized void | addItemListener(java.awt.event.ItemListener l)Adds the specified item listener to receive item events from
this Choice menu. Item events are sent in response
to user input, but not in response to calls to select .
If l is null , no exception is thrown and no action
is performed.
Refer to AWT Threading Issues for details on AWT's threading model.
if (l == null) {
return;
}
itemListener = AWTEventMulticaster.add(itemListener, l);
newEventsOnly = true;
|
public void | addNotify()Creates the Choice 's peer. This peer allows us
to change the look
of the Choice without changing its functionality.
synchronized (getTreeLock()) {
if (peer == null)
peer = getToolkit().createChoice(this);
super.addNotify();
}
|
java.lang.String | constructComponentName()Constructs a name for this component. Called by
getName when the name is null .
synchronized (getClass()) {
return base + nameCounter++;
}
|
public int | countItems()
return pItems.size();
|
boolean | eventEnabled(java.awt.AWTEvent e)
if (e.id == ItemEvent.ITEM_STATE_CHANGED) {
if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0 ||
itemListener != null) {
return true;
}
return false;
}
return super.eventEnabled(e);
|
public javax.accessibility.AccessibleContext | getAccessibleContext()Gets the AccessibleContext associated with this
Choice . For Choice components,
the AccessibleContext takes the form of an
AccessibleAWTChoice . A new AccessibleAWTChoice
instance is created if necessary.
if (accessibleContext == null) {
accessibleContext = new AccessibleAWTChoice();
}
return accessibleContext;
|
public java.lang.String | getItem(int index)Gets the string at the specified index in this
Choice menu.
return getItemImpl(index);
|
public int | getItemCount()Returns the number of items in this Choice menu.
return countItems();
|
final java.lang.String | getItemImpl(int index)
return (String)pItems.elementAt(index);
|
public synchronized java.awt.event.ItemListener[] | getItemListeners()Returns an array of all the item listeners
registered on this choice.
return (ItemListener[])(getListeners(ItemListener.class));
|
public T[] | getListeners(java.lang.Class listenerType)Returns an array of all the objects currently registered
as FooListener s
upon this Choice .
FooListener s are registered using the
addFooListener method.
You can specify the listenerType argument
with a class literal, such as
FooListener.class .
For example, you can query a
Choice c
for its item listeners with the following code:
ItemListener[] ils = (ItemListener[])(c.getListeners(ItemListener.class));
If no such listeners exist, this method returns an empty array.
EventListener l = null;
if (listenerType == ItemListener.class) {
l = itemListener;
} else {
return super.getListeners(listenerType);
}
return AWTEventMulticaster.getListeners(l, listenerType);
|
public int | getSelectedIndex()Returns the index of the currently selected item.
If nothing is selected, returns -1.
return selectedIndex;
|
public synchronized java.lang.String | getSelectedItem()Gets a representation of the current choice as a string.
return (selectedIndex >= 0) ? getItem(selectedIndex) : null;
|
public synchronized java.lang.Object[] | getSelectedObjects()Returns an array (length 1) containing the currently selected
item. If this choice has no items, returns null .
if (selectedIndex >= 0) {
Object[] items = new Object[1];
items[0] = getItem(selectedIndex);
return items;
}
return null;
|
public void | insert(java.lang.String item, int index)Inserts the item into this choice at the specified position.
Existing items at an index greater than or equal to
index are shifted up by one to accommodate
the new item. If index is greater than or
equal to the number of items in this choice,
item is added to the end of this choice.
If the item is the first one being added to the choice,
then the item becomes selected. Otherwise, if the
selected item was one of the items shifted, the first
item in the choice becomes the selected item. If the
selected item was no among those shifted, it remains
the selected item.
synchronized (this) {
if (index < 0) {
throw new IllegalArgumentException("index less than zero.");
}
/* if the index greater than item count, add item to the end */
index = Math.min(index, pItems.size());
insertNoInvalidate(item, index);
}
// This could change the preferred size of the Component.
if (valid) {
invalidate();
}
|
private void | insertNoInvalidate(java.lang.String item, int index)Inserts an item to this Choice ,
but does not invalidate the Choice .
Client methods must provide their own synchronization before
invoking this method.
if (item == null) {
throw new
NullPointerException("cannot add null item to Choice");
}
pItems.insertElementAt(item, index);
ChoicePeer peer = (ChoicePeer)this.peer;
if (peer != null) {
peer.addItem(item, index);
}
// no selection or selection shifted up
if (selectedIndex < 0 || selectedIndex >= index) {
select(0);
}
|
protected java.lang.String | paramString()Returns a string representing the state of this Choice
menu. This method is intended to be used only for debugging purposes,
and the content and format of the returned string may vary between
implementations. The returned string may be empty but may not be
null .
return super.paramString() + ",current=" + getSelectedItem();
|
protected void | processEvent(java.awt.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.
Note that if the event parameter is null
the behavior is unspecified and may result in an
exception.
if (e instanceof ItemEvent) {
processItemEvent((ItemEvent)e);
return;
}
super.processEvent(e);
|
protected void | processItemEvent(java.awt.event.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 .
Note that if the event parameter is null
the behavior is unspecified and may result in an
exception.
ItemListener listener = itemListener;
if (listener != null) {
listener.itemStateChanged(e);
}
|
private void | readObject(java.io.ObjectInputStream s)Reads the ObjectInputStream and if it
isn't null adds a listener to receive
item events fired by the Choice item.
Unrecognized keys or values will be ignored.
GraphicsEnvironment.checkHeadless();
s.defaultReadObject();
Object keyOrNull;
while(null != (keyOrNull = s.readObject())) {
String key = ((String)keyOrNull).intern();
if (itemListenerK == key)
addItemListener((ItemListener)(s.readObject()));
else // skip value for unrecognized key
s.readObject();
}
|
public void | remove(java.lang.String item)Removes the first occurrence of item
from the Choice menu. If the item
being removed is the currently selected item,
then the first item in the choice becomes the
selected item. Otherwise, the currently selected
item remains selected (and the selected index is
updated accordingly).
synchronized (this) {
int index = pItems.indexOf(item);
if (index < 0) {
throw new IllegalArgumentException("item " + item +
" not found in choice");
} else {
removeNoInvalidate(index);
}
}
// This could change the preferred size of the Component.
if (valid) {
invalidate();
}
|
public void | remove(int position)Removes an item from the choice menu
at the specified position. If the item
being removed is the currently selected item,
then the first item in the choice becomes the
selected item. Otherwise, the currently selected
item remains selected (and the selected index is
updated accordingly).
synchronized (this) {
removeNoInvalidate(position);
}
// This could change the preferred size of the Component.
if (valid) {
invalidate();
}
|
public void | removeAll()Removes all items from the choice menu.
synchronized (this) {
if (peer != null) {
((ChoicePeer)peer).removeAll();
}
pItems.removeAllElements();
selectedIndex = -1;
}
// This could change the preferred size of the Component.
if (valid) {
invalidate();
}
|
public synchronized void | removeItemListener(java.awt.event.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.
Refer to AWT Threading Issues for details on AWT's threading model.
if (l == null) {
return;
}
itemListener = AWTEventMulticaster.remove(itemListener, l);
|
private void | removeNoInvalidate(int position)Removes an item from the Choice at the
specified position, but does not invalidate the Choice .
Client methods must provide their
own synchronization before invoking this method.
pItems.removeElementAt(position);
ChoicePeer peer = (ChoicePeer)this.peer;
if (peer != null) {
peer.remove(position);
}
/* Adjust selectedIndex if selected item was removed. */
if (pItems.size() == 0) {
selectedIndex = -1;
} else if (selectedIndex == position) {
select(0);
} else if (selectedIndex > position) {
select(selectedIndex-1);
}
|
public synchronized void | select(int pos)Sets the selected item in this Choice menu to be the
item at the specified position.
Note that this method should be primarily used to
initially select an item in this component.
Programmatically calling this method will not trigger
an ItemEvent . The only way to trigger an
ItemEvent is by user interaction.
if ((pos >= pItems.size()) || (pos < 0)) {
throw new IllegalArgumentException("illegal Choice item position: " + pos);
}
if (pItems.size() > 0) {
selectedIndex = pos;
ChoicePeer peer = (ChoicePeer)this.peer;
if (peer != null) {
peer.select(pos);
}
}
|
public synchronized void | select(java.lang.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.
Note that this method should be primarily used to
initially select an item in this component.
Programmatically calling this method will not trigger
an ItemEvent . The only way to trigger an
ItemEvent is by user interaction.
int index = pItems.indexOf(str);
if (index >= 0) {
select(index);
}
|
private void | writeObject(java.io.ObjectOutputStream s)Writes default serializable fields to stream. Writes
a list of serializable ItemListeners
as optional data. The non-serializable
ItemListeners are detected and
no attempt is made to serialize them.
s.defaultWriteObject();
AWTEventMulticaster.save(s, itemListenerK, itemListener);
s.writeObject(null);
|