Question

My GUI have several JSpinner instances, and I have an EnumMap. I want to "bind" each spinner to my Map, I mean, if jSpinner1 change his value, it should also change the value of the map with key1.

I don't want to do this manually for each jSpinners :

private void jSpinner1StateChanged(javax.swing.event.ChangeEvent evt) {

    Integer val = (Integer) jSpinner1.getValue(); 
    data.setValue(enumKey1, val);
}   

How could I do it correctly?


Yes, a factory is the best solution I think:

spinners = Arrays.asList(/*add jSpinners in the same order as the KeySet*/);


private void jSpinnerFactory() {
    int i=0;
    for (final Pic key : Pic.values()) {       //Pic.values(): keySet
        JSpinner js = spinners.get(i);
        js.addChangeListener(new javax.swing.event.ChangeListener() {
            @Override
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinnerStateChanged(evt,key);
            }
        });

        i++;
    }
}

private void jSpinnerStateChanged(javax.swing.event.ChangeEvent evt, Pic key) {                                        
    JSpinner js = (JSpinner) evt.getSource();
    data.setValue(key, (Integer) js.getValue());   //data: the object that have the EnumMap
}
Was it helpful?

Solution

You might simply extend JSpinner for a BoundSpinner, but I think I'd prefer a factory method to configure each field.

OTHER TIPS

For that purposes you can extend JSpinner which will store key for your map(in my example it's ObjectSpinner). Values is example enum, and I use ChangeListener for changing value in map.

Here is simple example for you :

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EnumMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Example {

    private enum Values{
        v1,v2,v3;
    }

    private EnumMap<Values, Integer> map = new EnumMap<>(Values.class);

    public static void main(String[] args) {
        new Example();
    }

    public Example() {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new JPanel();
        for(Values v : Values.values()){
            ObjectSpinner<Values> createSpinner = createSpinner(v);
            p.add(createSpinner);
        }

        JButton printResult = new JButton("printResult");
        printResult.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                System.out.println(map.values());
            }
        });

        frame.getContentPane().add(p);
        frame.getContentPane().add(printResult,BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }

    private ObjectSpinner<Values> createSpinner(Values value) {
        ObjectSpinner<Values> sp = new ObjectSpinner<Values>(value);
        map.put(value, null);
        sp.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent arg0) {
                ObjectSpinner<Values> spinner = (ObjectSpinner<Values>) arg0.getSource();
                map.put(spinner.getObject(), (Integer) spinner.getValue());
            }
        });
        return sp;
    }

    class ObjectSpinner<T> extends JSpinner{

        private T object;

        public ObjectSpinner(T object){
            super();
            this.object = object;
        }

        public T getObject(){
            return object;
        }

    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top