1 package tim.prune.save;
3 import javax.swing.JButton;
4 import javax.swing.ListSelectionModel;
5 import javax.swing.event.ListSelectionEvent;
6 import javax.swing.event.ListSelectionListener;
10 * Class to enable and disable a pair of up and down buttons
12 public class UpDownToggler implements ListSelectionListener
14 private JButton _upButton = null;
15 private JButton _downButton = null;
16 private int _maxIndex = 2;
19 * Constructor giving buttons to enable/disable
20 * @param inUpButton up button
21 * @param inDownButton down button
23 public UpDownToggler(JButton inUpButton, JButton inDownButton)
25 _upButton = inUpButton;
26 _downButton = inDownButton;
31 * @param inListSize number of items in list
33 public void setListSize(int inListSize)
35 _maxIndex = inListSize - 1;
39 * list selection has changed
41 public void valueChanged(ListSelectionEvent e)
43 ListSelectionModel lsm = (ListSelectionModel) e.getSource();
44 if (lsm.isSelectionEmpty())
46 // no rows are selected
47 _upButton.setEnabled(false);
48 _downButton.setEnabled(false);
52 // single row is selected
53 int row = lsm.getMinSelectionIndex();
54 _upButton.setEnabled(row > 0);
55 _downButton.setEnabled(row >= 0 && row < _maxIndex);