• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Key Events firing for non displayed controls

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am having a problem with Key Listeners. I have an application that has a JTabbedPane. On 1 page I have several JButtons with Key Listeners attached to them. I detect if the enter key has been struck, If so I execute some code. On the second page I have a JList with a Key Listener attached, If a user hilights a selection and presses enter, some code gets executed.
Here is the problem. I am tabbing throught the app with the tab key. When I get to the list and the list has focus and is hi-lighted, So does a JButton on the first page, when I hit the enter key on the list, the proper code gets executed, but then somehow the Key Listener for the Jbutton on the first page get fired and that code executes. I am not even on that page. Why does the code get executed ! I can put a ugly work around and check to make sure the user is on the page and only execute the code if the page is selected, but I would like a more elegant solution.
Any thoughts would be much appreciated.
Thanks,
John Palmieri
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using the same KeyListener for your buttons and the List?
Perhaps, you can check for the source of the event to see who fire the event that you were not specting. (e.getSource()).
I know this is not a real elegant solution, but you can check to see if the source is an instanceof JList or JButton.
If you use an annonimous listener for your components, when the list fires an actionEvent, the button should not see it.
<pre>
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {}
});
list.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {}
});
</pre>
 
John Palmieri
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the Key listeners are different. I set one up for the JButton and one for the JList.
 
Friends help you move. Good friends help you move bodies. This tiny ad will help:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic