aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JList initial focus issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JList initial focus issue" Watch "JList initial focus issue" New topic
Author

JList initial focus issue

Aodhan Hoffman
Ranch Hand

Joined: May 21, 2004
Posts: 35

My apologies if this has been answered elsewhere, I failed to find it if it had been.

I have a swing app that has a text area populated by action handler for one jlist, a section list ( inside a scroll pane, inside a splitpane ) which is populated by the action handler for another jlist, a chapter list ( likewise in a scroll pane inside other half of splitpane ) all of which are in a jframe.

THe problem is that on first launch, I can change the careted selection in either jlist using the keyboard, and tab between all three components, but no action is taken until I click into each component.

the chapter list doesn't react until I click into it, so the section list doesn't react. I click into the section list and finally things begin to shake. Only once I've /clicked/ into /both/ the chapter list and the section list can I expect the text area to change by navigating the chapter list.

I am requesting focus into the chapter list and giving it a selected index upon building the GUI.


Thoughts?


-------<br /> Aodhan H - Ad Astra Per Aspera<br /> -------
Aodhan Hoffman
Ranch Hand

Joined: May 21, 2004
Posts: 35

in creating my chapter jlist, my last step is

jlChapterList.setSelectedIndex(0);
jlChapterList.requestFocus();// set focus on chapter list

frame.setVisible( true ); // make window visible
But while the first list entry is highlighted, and using the arrow keys changes selection, there's no action registered. My action handler for the jlist ( public void valueChanged( ListSelectionEvent e ) ), only fires once I've clicked into the component.

I'm know this is a relatively common mistake/problem, but I don't see any answers beyond what I've already done.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

It's not a mistake and calling it a problem is probably relative to everyone's situation. The JList component was built this way on purpose. The only way to fire the event for the JList is by hitting the enter key or clicking it with the mouse.

The only other way around this that I know of is you would have to create a FocusListener and add that listener to each item in your JList's model. Then on focusGained() you can do whatever you want to do about that item being selected.


GenRocket - Experts at Building Test Data
Aodhan Hoffman
Ranch Hand

Joined: May 21, 2004
Posts: 35

thanks for the info, I could have sworn there are swing apps that don't work like this, but I haven't done a specific test.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Originally posted by Aodhan Hoffman:
thanks for the info, I could have sworn there are swing apps that don't work like this, but I haven't done a specific test.


How would you know they don't work like this? If you used my suggestion for and used a focus listener, you would never know the difference unless you looked at the code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JList initial focus issue