I have an ArrayList<Integer>
and I want to use two jspinners
, one for getting the index of the ArrayList
and the other for displaying the corresponding integer.
Asked
Active
Viewed 818 times
-1

Carrie Kendall
- 11,124
- 5
- 61
- 81

Sotoy
- 13
- 4
-
1Why use a spinner for the corresponging element from the list? It seems a JLabel would be a better choice. Also, too broad. – tobias_k Dec 20 '14 at 18:50
-
it is compulsory that I use a spinner. – Sotoy Dec 20 '14 at 19:12
-
Do you need that changing the value on the second spinner will change the index on the first spinner, or only that changing the index on the first spinner will change the value on the second spinner? – Boann Dec 20 '14 at 19:21
-
Only changing the index on the first spinner will change the value on the second spinner! – Sotoy Dec 20 '14 at 19:26
1 Answers
1
First limit the range of the first spinner to the size of the list prevent index-out-of-bounds exceptions:
jspinner1.setModel(new SpinnerNumberModel(0, 0, yourList.size() - 1, 1));
Then add the event handler that will set the second spinner when the first one is changed. On Java 8+:
jspinner1.addChangeListener((ChangeEvent e) -> {
jspinner2.setValue(yourList.get((int)jspinner1.getValue()));
});
On old versions of Java:
jspinner1.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
jspinner2.setValue(yourList.get((int)jspinner1.getValue()));
}
});

Boann
- 48,794
- 16
- 117
- 146