Java + swing + mvc: how to create a model that interacts with multiple controls?

I have this Swing application I'm working on, it has a pair of JSliders and JTextFields that display and control the value of two numbers. Right now, I kind of hacked everything together to make it work, intercept change events and update the display. However, I think the best way is to make a model with two numbers and somehow connect this model with my GUI. I know how to make a model, but how would I lose the way I could relate this to my GUI ... any suggestions?

update: my model is ready + includes changes in firing properties. I think I would like to use some kind of data binding approach (like JGoodies Binding), but I'm not sure how ... JGoodies Binding does not support sliders.

edit: also note when the slider changes position, the text field should change its value; when the text field changes the value, the slider should change the position; and when the model changes value, how the text box + slider should be updated.

alt text

+1
source share
2 answers

To synchronize two components, simply use EventListener to update the other. Here is an example of connecting JSlider and JSpinner :

public class SpinSlider extends JPanel {

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("SpinSlider");
                frame.add(new SpinSlider());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    public SpinSlider() {
        this.setLayout(new FlowLayout());
        final JSpinner spinner = new JSpinner();
        final JSlider slider = new JSlider();
        slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSlider s = (JSlider) e.getSource();
                spinner.setValue(s.getValue());
            }
        });
        this.add(slider);
        spinner.setModel(new SpinnerNumberModel(50, 0, 100, 1));
        spinner.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSpinner s = (JSpinner) e.getSource();
                slider.setValue((Integer) s.getValue());
            }
        });
        this.add(spinner);
    }
}
+2

.

, :

public class MyView extends JPanel /*JFrame, whatever*/ {
    private MyModel myModel;

    public MyView(MyModel myModel) {
        this.myModel = myModel;
        // build GUI
        // ...
        textfield1.setText(String.valueOf(myModel.getValue1()));
    }

    // this is what you do when a control changes:
    public void value1Changed(int newValue) {
        myModel.setValue1(newVvalue);
    }
}

public class MyModel {
    private int value1;

    // getters, setters
    // ...
}
+1

All Articles