When I run this code the selected item is not visible. I've already tried to run it in a separate thread with no luck.
import javax.swing.JFrame;
import java.awt.Container;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.JScrollPane;
import java.awt.Dimension;
public class ScrollList extends JFrame {
int defaultValue;
ScrollList() {
Container cp = getContentPane();
JList list = createList();
defaultValue = 20;
cp.add(createScrollPane(list));
pack();
setVisible(true);
list.ensureIndexIsVisible(defaultValue);
}
JList createList() {
Integer[] model = new Integer[73];
JList list = new JList(model);
for (int i = 1; i < model.length; i++)
model[i] = i;
list.setSelectedIndex(defaultValue);
return list;
}
JScrollPane createScrollPane(JList list) {
JScrollPane s = new JScrollPane(createList());
s.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
s.setPreferredSize(new Dimension(100, 200));
return s;
}
public static void main(String[] args) {
new ScrollList();
}
}
Thanks for helping in advance!