This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JList initial focus issue

 
Aodhan Hoffman
Ranch Hand
Posts: 35
Java MyEclipse IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Aodhan Hoffman
Ranch Hand
Posts: 35
Java MyEclipse IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aodhan Hoffman
Ranch Hand
Posts: 35
Java MyEclipse IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic