• Post Reply Bookmark Topic Watch Topic
  • New Topic

FocusListener

 
Mike Southgate
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short version of question: how does one ensure that the focusLost processing of a FocusListener is done prior to the component that's gaining the focus changing its state because of the focus?

Long version:
I have a dialog with a JTextArea and a JList. When the user selects an item in the JList, the text area is populated from the JList item's contents. This works fine.
If the user changes the text in the text area, the selected item in the JList needs to be updated. So I've created a FocusListener on the text area with a focusLost method that goes to the JList, determines which item is selected and updates it. This works fine when the component gaining the focus is NOT the JList. When it is the JList, it works about half of the time. The other half of the time the item that's text is updated is the newly selected item, instead of the old one.

Since the documentation says the focusLost event is fired before the focusGained event, I'm assuming that these events are on separate threads. Is this correct, and if so how does one synchronize for this when the method doing the focusGained is not mine?

thanks

mike
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would steer clear of focusListeners.

If it is a change in the text area that you want to trigger a change in the JList,
have you thought of a document listener?
 
H Lander
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever new text is loaded into your text area record the index of the JList item to which that text corresponds. When the text area loses focus, update the list item to which the text corresponds (whose index you you have recorded). This may or may not be the currently selected item. If you do this, then you do not need to worry about the order of the events.

Good luck,
Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!