Showing posts with label jlist. Show all posts
Showing posts with label jlist. Show all posts

Thursday, May 30, 2013

Java JList scroll to selected item

Very funny, just one line of code make it fine.


String[] data = {"one", "two", "three", "four", /* AND A LOT MORE */};
JList dataList = new JList(data);
JScrollPane scrollPane = new JScrollPane(dataList);

/* And scroll to selected index */
dataList.ensureIndexIsVisible(dataList.getSelectedIndex());