aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Key Events firing for non displayed controls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Key Events firing for non displayed controls" Watch "Key Events firing for non displayed controls" New topic
Author

Key Events firing for non displayed controls

John Palmieri
Greenhorn

Joined: Jul 05, 2001
Posts: 8
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
Alex Draper
Greenhorn

Joined: Nov 19, 2001
Posts: 8
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

Joined: Jul 05, 2001
Posts: 8
No, the Key listeners are different. I set one up for the JButton and one for the JList.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Key Events firing for non displayed controls