Class Checkbox

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

public class Checkbox extends Component implements ItemSelectable, Accessible
Emulator for Checkbox — the AWT 1.0 checkbox, not JCheckBox. A thin delegating shell over its SCheckbox peer, with the CheckboxGroup bookkeeping that turns a checkbox into a radio button:
Checkbox verbose = new Checkbox("Verbose", false);
verbose.addItemListener(e -> setVerbose(e.getStateChange() == ItemEvent.SELECTED));
verbose.setState(true);        // silent: no ItemEvent, exactly as in AWT

CheckboxGroup units = new CheckboxGroup();
new Checkbox("metric", true, units);      // grouped → renders as a radio
new Checkbox("imperial", false, units);

Only the browser posts events

Every programmatic path here is silent — setState(boolean), the ctors, setCheckboxGroup(CheckboxGroup) and every CheckboxGroup mutator. In AWT only the toolkit posts an ItemEvent, which inverts Swing, where AbstractButton.setSelected drives a ButtonModel fan-out. A browser toggle posts exactly one event, on the clicked box: the sibling a group cascade turns off gets nothing.

Two clicks post nothing at all, both because the group vetoes the state change: unchecking the box that is its group's current selection, and re-clicking that box. Both bounce the peer back to checked.

Responsibility split with the peer:

  • Surrogate — the rendered Vaadin Checkbox, the from-client value → ItemEvent fan-out, and the radio glyph (appearance only).
  • Emulator (here) — the AWT-shape API migrated code compiles against, the label/state/group fields, and all group semantics: :surrogates knows nothing about groups (D_buttongroup_browser_click).

Leaf peer is locked down per R_leaf_peer_lockdown: java.awt.Checkbox has no public subclass in java.awt (CheckboxMenuItem is a MenuItem, an unrelated MenuComponent branch), so there is no protected (Component peer) ctor and every instance peers over an SCheckbox. The checkbox-vs-radio duality does not force a peer seam — AWT reads the glyph off group != null at paint time, so it is a live property of one peer, not two peer types.

See Also:
  • Constructor Details

  • Method Details

    • addNotify

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

      public String getLabel()
    • setLabel

      public void setLabel(String label)
    • getState

      public boolean getState()
    • setState

      public void setState(boolean state)
      Sets the checked state, silently — no ItemEvent, in any of the paths below.
      Parameters:
      state - false is ignored when this box is its group's current selection: AWT upgrades it back to true rather than leaving the group with nothing selected. Passing true on a grouped box turns its sibling off.
    • getSelectedObjects

      public Object[] getSelectedObjects()
      Specified by:
      getSelectedObjects in interface ItemSelectable
      Returns:
      a one-element array holding the label — which may itself be null — when checked, else null. AWT's convention here, which java.awt.List does not share (it answers an empty array)
    • getCheckboxGroup

      public CheckboxGroup getCheckboxGroup()
    • setCheckboxGroup

      public void setCheckboxGroup(CheckboxGroup g)
      Re-homes this box into g, switching the glyph to match.

      Leaves the old group with no selection at all when this box was holding it. That is AWT's behaviour and not an oversight: the clearing call runs after the box has already been re-homed, so setCurrent's still-one-of-mine guard skips the deselect and only the pointer is dropped.

      Parameters:
      g - null removes the box from its group; the same group it already belongs to is an early return
    • 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 toggle reaches the listeners through — both the peer bridge and a user-code processEvent call. AWT subclasses override this (calling super) to intercept toggles 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