• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

KeyListener is not respond

 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I created a JDialog with a JComboBox
I add KeyListener support for the JComboBox but there is no response whenever I press any button on the keyboard.

Here is the code:








Any suggestion why it’s not responding ?


Thanks
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the combo box have focus? It will need to be the focused item in order for it to process key input.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, depending on what you are trying to accomplish with a KeyListener on a JComboBox you might not be adding the listener to the appropriate element.
 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea is to enable the button when I type any key in the JComboBox
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
angus young wrote:The idea is to enable the button when I type any key in the JComboBox


Then you probably need to assign the key listener to the editor of the JComboBox. In edit mode, a JComboBox uses a text field and that is the element you need to add the listener to since it is what gains focus.
 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like this : findBox.getEditor().addActionListener(this); ?
 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like this : findBox.getEditor().addActionListener(this); ?
 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like this : findBox.getEditor().addActionListener(this); ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic