• Post Reply Bookmark Topic Watch Topic
  • New Topic

JComboBox ItemListener passing typed key to JTextField

 
johnsmit johnsmih
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a problem trying to change a GUI when the user selects a certain JComboBox list item. Everything works great if user clicks the item with a mouse, or with the arrow keys. However, when using the key accelerators, the issue arises.

Basically, I have a JComboBox with an "Other" list item. If the user selects this item, I wish to hide the combo box and display a text field. The issue is that if the user clicks the "O" key on the keyboard to select "Other", then an "O" is transferred/copied/whatever to the text field. I have tried setting the text field to null in the item listener for the JComboBox and also in the focusGained of the text field. Nothing works.

Has anyone ever had the need to do something like this? How did you solve it? I am open to all suggestions. It's driving me crazy

Here is some example code (taken from Java's JComboBoxDemo). You will find that if you use the mouse to select "Other" or use the arrow keys, everything is fine. If you type "O", you will see an "O" in the text field.

 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) The basic problem is you can't request focus on a component that is not visible.

2) The second problem is that requestFocus() is the wrong method to use. Read the API description and it will tell you the proper method.

3) There is no need to create and dispatch a FocusEvent.

So taking all the above into account the code should be something like:



Also, an ItemListener doesn't work very well in this case because when you use the up/down arrow keys to navigate and event is fired every time. The event should only be fired when the user actually selectects something. The trick is to use an ActionListener instead of the ItemListener. Then you can set this property on the combo box:

 
johnsmit johnsmih
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply; but it changed nothing. I.e., after implementing your changes, I still have the original issue. The "O" is added/copied/whatever to the text field. Does your example behave like this? I have also tried consuming the key event, but that does not work, either.

New Code with your prescribed changes:
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
johnsmit johnsmih wrote:

Please read your private messages and follow the instructions therein.
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works fine for me using JDK6_7 on XP.

The only other thing I can suggest is to wrap 4 lines of code in a SwingUtilties.invokeLater().
 
johnsmit johnsmih
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure why, but invokeLater solved it. Thanks a lot for your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!