This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Set focus on selected item in scrollable JList Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Set focus on selected item in scrollable JList" Watch "Set focus on selected item in scrollable JList" New topic
Author

Set focus on selected item in scrollable JList

Robin Sharma
Ranch Hand

Joined: Aug 24, 2005
Posts: 76
Howdy!

I am using a Jlist to display a list of users. The structure is as below:
DefaultListModel(containing the list of users (Strings))->JList -> JScrollPane -> JPanel -> JDialog.

Now i want that when the focus is on the list and the user presses a key (alphabet like 'a' or 'B') the list should auto-scroll to the first name that starts with that alphabet.

I am able to set the selected index in the list (using a key listener attached the list) but somehow if the name is not visible in the list at that moment, the list doesn't scroll to that name .

Do i need to add some listener to the scroll pane also?

Thanks.


DW
There is always a bug :-)
Robin Sharma
Ranch Hand

Joined: Aug 24, 2005
Posts: 76
JList.ensureIndexIsVisible(int index) to the rescue
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Set focus on selected item in scrollable JList
 
Similar Threads
Key Events firing for non displayed controls
JList enter key advice needed
JList initial focus issue
Disabling Keybinding For Particular Component
JTree + JList in JScrollPane - Out of Sync