The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > JSpinner  [8 examples]

e792. Creating a SpinnerListModel That Loops Through Its Values

By default, if the user is browsing the values in a SpinnerListModel, the iteration stops when either end is reached. This example demonstrates a subclass that allows the user to continuously loop through the values.
    SpinnerCircularListModel listModel = new SpinnerCircularListModel(
        new String[]{"red", "green", "blue"});
    JSpinner spinner = new JSpinner(listModel);
    
    public class SpinnerCircularListModel extends SpinnerListModel {
        public SpinnerCircularListModel(Object[] items) {
            super(items);
        }
    
        // Returns the next value. If the current value is at the end
        // of the list, returns the first value.
        // There must be at least one item in the list.
        public Object getNextValue() {
            java.util.List list = getList();
            int index = list.indexOf(getValue());
    
            index = (index >= list.size()-1) ? 0 : index+1;
            return list.get(index);
        }
    
        // Returns the previous value. If the current value is at the
        // start of the list, returns the last value.
        // There must be at least one item in the list.
        public Object getPreviousValue() {
            java.util.List list = getList();
            int index = list.indexOf(getValue());
    
            index = (index <= 0) ? list.size()-1 : index-1;
            return list.get(index);
        }
    }

 Related Examples
e786. Creating a JSpinner Component
e787. Creating an Hour JSpinner Component
e788. Disabling Keyboard Editing in a JSpinner Component
e789. Limiting the Values in a Number JSpinner Component
e790. Setting the Margin Space on a JSpinner Component
e791. Customizing the Editor in a JSpinner Component
e793. Listening for Changes to the Value in a JSpinner Component

See also: Actions    JButton    JCheckBox    JComboBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JLabel    JList    JProgressBar    JRadioButton    JScrollPane    JSlider    JSplitPane    JTabbedPane    JToolBar    Keystrokes and Input Maps    Layout    Look and Feel    Menus    Progress Monitor    The Screen    Tool Tips    UI Default Values   


© 2002 Addison-Wesley.