Question

I'm working on a simple java program (using NetBeans' swing GUI builder). You're supposed to select a randomly placed item from the JComboBox as fast as you can, then it will tell you your time and save it (keeping statistics and whatnot).

Problem is, once you select an item and go back to find a different item (in a new random position in the JComboBox), the scrollbar seems to have remembered it's previous position.

Since this game is supposed to be competitive, it wouldn't really be fair for the scrollbar to be in the previous player's position.

Is there any way to set the position of the scrollbar in a JComboBox?

P.S. it's a vertical scrollbar. Also, I did try to find the answer online, but couldn't find it after over two hours of searching. Any help appreciated!

Was it helpful?

Solution

Select the first item so the scrollbar goes back to the top, then set the selection to -1.

comboBox.setSelectedIndex(0);
comboBox.setSelectedIndex(-1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top