@SuppressWarnings("LeakingThisInConstructor") public DrawControls(DrawPanel target) { this.target = target; setLayout(new FlowLayout()); setBackground(Color.lightGray); target.setForeground(Color.red); CheckboxGroup group = new CheckboxGroup(); Checkbox b; add(b = new Checkbox(null, group, false)); b.addItemListener(this); b.setForeground(Color.red); add(b = new Checkbox(null, group, false)); b.addItemListener(this); b.setForeground(Color.green); add(b = new Checkbox(null, group, false)); b.addItemListener(this); b.setForeground(Color.blue); add(b = new Checkbox(null, group, false)); b.addItemListener(this); b.setForeground(Color.pink); add(b = new Checkbox(null, group, false)); b.addItemListener(this); b.setForeground(Color.orange); add(b = new Checkbox(null, group, true)); b.addItemListener(this); b.setForeground(Color.black); target.setForeground(b.getForeground()); Choice shapes = new Choice(); shapes.addItemListener(this); shapes.addItem("Lines"); shapes.addItem("Points"); shapes.setBackground(Color.lightGray); add(shapes); }
@Override public void itemStateChanged(final ItemEvent e) { // group.setSelectedCheckbox() will repaint the component // to let LWCheckboxPeer correctly handle it we should call it // after the current event is processed SwingUtilities.invokeLater(new Runnable() { @Override public void run() { boolean postEvent = true; final CheckboxGroup group = getTarget().getCheckboxGroup(); if (group != null) { if (e.getStateChange() == ItemEvent.SELECTED) { if (group.getSelectedCheckbox() != getTarget()) { group.setSelectedCheckbox(getTarget()); } else { postEvent = false; } } else { postEvent = false; if (group.getSelectedCheckbox() == getTarget()) { // Don't want to leave the group with no selected // checkbox. getTarget().setState(true); } } } else { getTarget().setState(e.getStateChange() == ItemEvent.SELECTED); } if (postEvent) { postEvent(new ItemEvent(getTarget(), ItemEvent.ITEM_STATE_CHANGED, getTarget().getLabel(), e.getStateChange())); } } }); }
@Override public void setCheckboxGroup(final CheckboxGroup g) { synchronized (getDelegateLock()) { getDelegate().getCurrentButton().removeItemListener(this); getDelegate().setRadioButton(g != null); getDelegate().getCurrentButton().addItemListener(this); } repaintPeer(); }
/** * Maps {@code Checkbox.getCheckboxGroup()} through queue */ public CheckboxGroup getCheckboxGroup() { return (runMapping(new MapAction<CheckboxGroup>("getCheckboxGroup") { @Override public CheckboxGroup map() { return ((Checkbox) getSource()).getCheckboxGroup(); } })); }
/** * Maps {@code Checkbox.setCheckboxGroup(CheckboxGroup)} through queue */ public void setCheckboxGroup(final CheckboxGroup grp) { runMapping(new MapVoidAction("setCheckboxGroup") { @Override public void map() { ((Checkbox) getSource()).setCheckboxGroup(grp); } }); }
public void setCheckboxGroup( CheckboxGroup group ) { if(this.group == group) return; // if we change from a checkbox to a radio button or vice versa if((this.group == null) != (group == null)) { this.group = group; callInit(); setup(); } this.group = group; }