Creating a SpinnerListModel That Loops Through Its Values
import java.util.List;
import javax.swing.JSpinner;
import javax.swing.SpinnerListModel;
public class Main {
public static void main(String[] argv) throws Exception {
SpinnerCircularListModel listModel = new SpinnerCircularListModel(
new String[] { "red", "green", "blue" });
JSpinner spinner = new JSpinner(listModel);
}
}
class SpinnerCircularListModel extends SpinnerListModel {
public SpinnerCircularListModel(Object[] items) {
super(items);
}
public Object getNextValue() {
List list = getList();
int index = list.indexOf(getValue());
index = (index >= list.size() - 1) ? 0 : index + 1;
return list.get(index);
}
public Object getPreviousValue() {
List list = getList();
int index = list.indexOf(getValue());
index = (index <= 0) ? list.size() - 1 : index - 1;
return list.get(index);
}
}
Related examples in the same category