The Java Developers Almanac 1.4


Order this book from Amazon.

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

e791. Customizing the Editor in a JSpinner Component

This example replaces the default editor (a JFormattedTextField) in a spinner component with a custom editor. The custom editor is simply a panel that displays a color. The name of the color to display is stored in a SpinnerListModel.
    // Create a color spinner
    ColorSpinner spinner = new ColorSpinner(
        new String[]{"red", "green", "blue"});
    
    // Change the selected color
    spinner.setValue("blue");
    
    public class ColorSpinner extends JSpinner {
        public ColorSpinner(String[] colorNames) {
            super();
            setModel(new SpinnerListModel(colorNames));
            setEditor(new Editor(this));
        }
    
        public class Editor extends JPanel implements ChangeListener {
            int preferredWidth = 30;
            int preferredHeight = 16;
    
            Editor(JSpinner spinner) {
                // Add the listener
                spinner.addChangeListener(this);
    
                // Set the preferred size
                setPreferredSize(new Dimension(preferredWidth, preferredHeight));
    
                // Display the current color
                setColor((String)spinner.getValue());
            }
    
            // Handles changes to the value stored in the model
            public void stateChanged(ChangeEvent evt) {
                JSpinner spinner = (JSpinner)evt.getSource();
    
                // Get the new value
                String value = (String)spinner.getValue();
    
                // Update the displayed color
                setColor(value);
            }
    
            // Updates the displayed color to 'colorName' which must be one
            // of the predefined colors in java.awt.Color.
            public void setColor(String colorName) {
                try {
                    // Find the field and value of colorName
                    Field field = Class.forName("java.awt.Color").getField(colorName);
                    Color color = (Color)field.get(null);
    
                    // Display the color
                    setBackground(color);
                } catch (Exception e) {
                }
            }
        }

 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
e792. Creating a SpinnerListModel That Loops Through Its Values
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.