Class Choice

All Implemented Interfaces:
ImageObserver, ItemSelectable, MenuContainer, Serializable, Accessible

public class Choice extends Component implements ItemSelectable, Accessible
Emulator for Choice — the AWT 1.0 dropdown, not JComboBox. Holds the JDK's item vector and selection itself, so every getter answers on any thread without reaching the SChoice peer, which renders them:
Choice units = new Choice();
units.add("metric");
units.add("imperial");                 // "metric" is selected — the first add selects it
units.addItemListener(e -> recompute((String) e.getItem()));
units.select("imperial");              // silent: no ItemEvent, exactly as in AWT

The JDK class is a Vector<String> and an int: no model, no editable mode, no renderer, no popup API and no ActionListener. Everything a migrator touches beyond the members below — setBounds, setFont, setEnabled, addFocusListener, requestFocus — is inherited Component.

The JDK's internal call directions are kept (R_no_vaadin_in_api limb 2): the modern names delegate to the deprecated ones, an insert or remove re-selects through the public select(int), and getSelectedItem() reads through the public getItem(int), so an override of any of them sees the same traffic it would on the desktop.

Leaf peer is locked down per R_leaf_peer_lockdown: java.awt.Choice has no public subclass in java.awt, so there is no protected (Component peer) ctor and every instance — including a user-code subclass — peers over an SChoice.

See Also:
  • Constructor Details

  • Method Details

    • addNotify

      public void addNotify()
      Overrides:
      addNotify in class Component
    • getItemCount

      public int getItemCount()
      Delegates to countItems(), not the other way round — that is the JDK's direction, so a subclass overriding the deprecated name stays on the invoked path (R_no_vaadin_in_api limb 2). Inverting it would leave such an override silently dead.
    • countItems

      @Deprecated public int countItems()
      Deprecated.
      as of JDK 1.1, replaced by getItemCount() — but see its note: this is the implementation, not the wrapper
    • getItem

      public String getItem(int index)
      Throws:
      ArrayIndexOutOfBoundsException - when out of range, as AWT's backing Vector does (R_match_swing_errors)
    • add

      public void add(String item)
      Appends an item; if it is the first, it becomes selected.

      Delegates to addItem(String), the JDK's direction — same R_no_vaadin_in_api limb 2 reasoning as getItemCount(). Note it does not route through insert(String, int): in the JDK both reach a private helper, so an insert override does not intercept add.

      Throws:
      NullPointerException - on a null item, as in AWT (R_match_swing_errors)
    • addItem

      @Deprecated public void addItem(String item)
      Deprecated.
      as of Java 2 v1.1, use add(String) — but this is the implementation, not the wrapper
    • insert

      public void insert(String item, int index)
      Inserts at index, shifting the rest up.

      Inserting at or before the selection re-selects index 0 — the JDK comments this as "selection shifted up" but the code does not shift. Reproduced verbatim in the peer.

      Parameters:
      index - clamped to the item count when too large, as in AWT
      Throws:
      IllegalArgumentException - on a negative index, as in AWT (R_match_swing_errors)
      NullPointerException - on a null item, as in AWT (R_match_swing_errors)
    • remove

      public void remove(String item)
      Removes the first occurrence.
      Throws:
      IllegalArgumentException - when the item is not present, as in AWT (R_match_swing_errors) — unlike select(String), which is silent
    • remove

      public void remove(int position)
      Throws:
      ArrayIndexOutOfBoundsException - when out of range, as AWT's backing Vector does (R_match_swing_errors)
    • removeAll

      public void removeAll()
      Empties the list; the selection becomes -1, assigned directly rather than through select.
    • getSelectedItem

      public String getSelectedItem()
      Returns:
      null when nothing is selected, which for AWT means "no items"
    • getSelectedObjects

      public Object[] getSelectedObjects()
      Specified by:
      getSelectedObjects in interface ItemSelectable
      Returns:
      a length-1 array, or null — not an empty array — when nothing is selected. AWT's convention, the opposite of Swing's.
    • getSelectedIndex

      public int getSelectedIndex()
    • select

      public void select(int pos)
      Selects by position. Fires no ItemEvent — in AWT only user interaction does. An ItemEvent arriving at a listener therefore always means "the user picked something", never "the program changed the selection".
      Throws:
      IllegalArgumentException - when out of range, as in AWT (R_match_swing_errors)
    • select

      public void select(String str)
      Selects the first item equal to str, or does nothing when there is no match — AWT is silent here, unlike remove(String). Fires no ItemEvent, same as select(int).
    • addItemListener

      public void addItemListener(ItemListener l)
      Specified by:
      addItemListener in interface ItemSelectable
    • removeItemListener

      public void removeItemListener(ItemListener l)
      Specified by:
      removeItemListener in interface ItemSelectable
    • getItemListeners

      public ItemListener[] getItemListeners()
    • processEvent

      protected void processEvent(AWTEvent e)
      Overrides:
      processEvent in class Component
    • processItemEvent

      protected void processItemEvent(ItemEvent e)
      The single funnel every selection reaches the listeners through — both the peer bridge and a user-code processEvent call. AWT subclasses override this (calling super) to intercept selections wholesale, so it must stay the only dispatch path.
    • paramString

      protected String paramString()
      Overrides:
      paramString in class Component
    • getAccessibleContext

      public AccessibleContext getAccessibleContext()
      Specified by:
      getAccessibleContext in interface Accessible
      Overrides:
      getAccessibleContext in class Component