Menu

#5 radiobutton functionality

open
nobody
None
5
2014-08-19
2004-07-08
Kris Raney
No

The following class can be used to get radiobutton
functionality from the checkboxes. Just construct,
passing a hashmap with the checkboxes as keys, and a
value for when that item is selected for values.

/
* @author kraney

/
public class RadioButtonListener implements
ValueChangedListener {
private HashMap checkboxes;
private Object current = null;
private boolean handlingEvent = false;

public RadioButtonListener(HashMap checkboxes) {
    this.checkboxes = checkboxes;
    for (Iterator i =

checkboxes.entrySet().iterator(); i.hasNext();) {
Entry e = (Entry)i.next();
CheckBox item = (CheckBox)e.getKey();
item.addListener(this);
if (current != null) {
item.setValue(false);
} else if (item.getValue()) {
current = e.getValue();
}
}
}

public Object getValue() {
    return current;
}

/* (non-Javadoc)
 * @see

jcurses.event.ValueChangedListener#valueChanged(jcurses.event.ValueChangedEvent)
*/
public void valueChanged(ValueChangedEvent event) {
if (handlingEvent) return;
try {
handlingEvent = true;
CheckBox changed = (CheckBox)event.getSource();
// don't let user uncheck all
if (!changed.getValue()) {
changed.setValue(true);
return;
}
current = checkboxes.get(changed);
for (Iterator i =
checkboxes.keySet().iterator(); i.hasNext();) {
CheckBox item = (CheckBox)i.next();
if (!item.equals(changed))
item.setValue(false);
}
} finally {
handlingEvent = false;
}
}
}

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.