I have a list of Map.Entry<String,Integer>
s that I am looping through, and for each one, making a JLabel
/JSpinner
representing that particular entry.
How can I make it so that when the ChangeListener
fires on the JSpinner
, it updates that entry to reflect the new value?
My code looks like
for (Map.Entry<String,Integer> entry : graph.getData()) {
SpinnerNumberModel model = new SpinnerNumberModel(
entry.getValue(),(Integer)0,(Integer)100,(Integer)5);
JSpinner spinner = new JSpinner(model);
JLabel label = new JLabel(entry.getKey());
spinner.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
entry.setValue((Integer)((JSpinner)e.getSource()).getValue());
}
});
// more stuff
}
However, this does not work, because entry either needs to be final or object-level, and it is neither.
Is there an easier way, like C#'s dataSource
that directly binds the object to the spinner? Or how can I make this work?