Question

I'm currently creating a GUI in NetBeans and I have a JSpinner that contains a list of colors, but I'm not sure how to make the text the corresponding color of each one.

I.E. - Red (should be colored red, etc.)

Below is the code so far for the JSpinner:

Color_Selector_Spinner = new javax.swing.JSpinner(); Color_Selector_Spinner.setModel(new javax.swing.SpinnerListModel(new String[] { "Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"}));

Was it helpful?

Solution

You need the following to make a color-coded JSpinner:

  1. A ChangeListener for the JSpinner to update the colors when the component value changes

  2. A translation of the String value into a Color.

There is only one JTextField used in the spinner so it must be updated with current color on every change event.

spinner.addChangeListener(new ChangeListener() {

   @Override
   public void stateChanged(ChangeEvent e) {

    try {
       String colorString = (String)spinner.getValue();

       Field field = Class.forName("java.awt.Color").getField(colorString.toLowerCase()); // toLowerCase because the color fields are RED or red, not Red
       Color color = (Color)field.get(null);

       JTextField tf = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
       tf.setForeground(color);

       } catch (Exception ex) { // handle ex }
   }
});

OTHER TIPS

I personally prefer to set my own editor instead of casting the existing editor and hoping it won't break in the future. So a slightly modified version of Reimeus code

import javax.swing.*;
import java.awt.Color;
import java.lang.reflect.Field;

public class ColorSpinnerDemo {

  public static void main( String[] args ) {
    JFrame testFrame = new JFrame( "TestFrame" );

    JSpinner spinner = new JSpinner( new SpinnerListModel( new String[]{"Red", "Orange", "Yellow", "Green"} ) );
    spinner.setEditor( new SpinnerEditor( spinner ).getComponent() );

    testFrame.add( spinner );

    testFrame.pack();
    testFrame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
    testFrame.setVisible( true );
  }

  private static class SpinnerEditor{
    private JLabel label = new JLabel(  );

    public SpinnerEditor( JSpinner spinner ){
      spinner.addChangeListener( new ChangeListener() {
        @Override
        public void stateChanged( ChangeEvent e ) {
          String currentValue = ( String ) ( ( JSpinner ) e.getSource() ).getValue();
          label.setText( currentValue );

          try {
            Field field = Class.forName("java.awt.Color").getField(currentValue.toLowerCase()); // toLowerCase because the color fields are RED or red, not Red
            Color color = (Color)field.get(null);

            label.setForeground( color );
          } catch ( NoSuchFieldException e1 ) {           
          } catch ( ClassNotFoundException e1 ) {
          } catch ( IllegalAccessException e1 ) {
          }
        }
      } );
    }

    public JComponent getComponent(){
      return label;
    }
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top