import javax.swing.JCheckBox;
/**
- * Class to represent a checkbox with three states, through which it cycles
- * Instead of calling isChecked, need to use getCurrentState which will
- * return 0, 1 or 2
+ * Class to represent a checkbox with multiple states, through which it cycles.
+ * Instead of calling isChecked, callers need to use getCurrentState which will
+ * return 0 up to (n-1) for n states.
*/
-public class TripleStateCheckBox extends JCheckBox implements ItemListener
+public class MultiStateCheckBox extends JCheckBox implements ItemListener
{
/** Array of icons to be used */
- private ImageIcon[] _icons = new ImageIcon[3];
- /** Current state 0, 1 or 2 */
+ private ImageIcon[] _icons = null;
+ /** Current state 0 to n-1 */
private int _currState = 0;
+ /** Number of states n */
+ private final int _numStates;
+
/** Inner class to proxy the listening events */
private class ProxyListener implements ItemListener
}
}
- /** Constructor */
- public TripleStateCheckBox()
+ /**
+ * Constructor
+ * @param inNumStates number of states to cycle through
+ */
+ public MultiStateCheckBox(int inNumStates)
{
+ _numStates = (inNumStates > 0) ? inNumStates : 1;
+ _icons = new ImageIcon[_numStates];
addItemListener(this);
}
- /** Set the current state */
+ /**
+ * @param inState state to set
+ */
public void setCurrentState(int inState)
{
- _currState = inState % 3;
+ _currState = inState % _numStates;
setIcon(_icons[_currState]);
setSelected(false);
- setSelectedIcon(_icons[(_currState+1)%3]);
+ setSelectedIcon(_icons[(_currState+1) % _numStates]);
}
- /** @return current state 0, 1 or 2 */
+ /**
+ * @return current state 0 to n-1
+ */
public int getCurrentState()
{
return _currState;
/**
* Set the icon to use for the given index
- * @param inIndex index 0, 1 or 2
+ * @param inIndex index 0 to n-1
* @param inIcon icon to use for that state
*/
public void setIcon(int inIndex, ImageIcon inIcon)
{
- _icons[inIndex % 3] = inIcon;
+ _icons[inIndex % _numStates] = inIcon;
}
@Override