Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

KeyListener Stops Working

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I've just moved this topic here, I entered it into the wrong forum(Java Intermediate), but I think Swing is more appropriate.


I've set up a KeyListener on a JFrame, which works initially, as can be seen by pressing keys.. output is written on console.

The JFrame has a JDesktopPane added to its content pane.

And the JDesktopPane, has a JInternalPane (shown in Red added).

When the program is run, you can press keys, and works, but once you press on the JInternalFrame, the KeyListener no longer works.

I don't have a clue how to fix this, can anyone help?
I've included the 3 classes below.

Thanks for any help.

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Would you be better off using key bindings instead? You can find out more about them here.

2) Your components can all have the same KeyListener by one of several ways. One is to pass the Main object to the containers via a parameter of their constructors. This has to be chained up through desktop and internal. Then have the components add the main as their keylistener. Note that you will have to add a keylistener onto Internal's contentPane for this to work.

3) Another way is to recursively iterate through all of main's components and add the KeyListener.

I still feel that when all is said and done, key bindings are what you really need.
[ August 26, 2007: Message edited by: pete stein ]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to mention one other issue. The reason that your keyListener wasn't working even after you closed the JInternalFrame was due to a focus issue. When you application started, the main application had the focus. When you clicked on the JInternalFrame, it received the focus and swallowed all the keystrokes. Finally, if you deleted the JInternalFrame, then the Desktop object had the focus, and it swallowed the keystrokes. Another way to get the keystrokes back would be to get the focus back to whatever needs the keystrokes.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I've tried using Key bindings


This works at first, but after I open, then remove the JInternal Frame, it no longer works, I don't get it.

Any ideas?
Thanks
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, again it's probably a problem of focus. After you delete the JInternalFrame the focus is no longer on the main application but on the Desktop class object. Either set the key bindings on the desktop class object or somehow get the focus back to the main application.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how to set the focus, its too confusing, and differs from operating system to operating system.

I tried, repeating the code above after I remove the JInternalFrame, but still nothing, it doesn't seem to be any better than keyListener.

Any other ideas? Thanks
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my recent search I've found that there are 3 inputMaps that can be used. You might try to use the one associated with "WHEN_IN_FOCUSED_WINDOW":



This will be active within your main JFrame and all of its subcomponents.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might try something like this:



A sheepish warning here though: This is the first time that I've really used key bindings. I've always understood that it is better to use these as opposed to keylisteners mainly because it is almost always better to go with a higher level construct rather than a lower level construct, but I'm learning about this beast right now for the first time. Please take what I have shown with a very large grain / handful of salt!
[ August 26, 2007: Message edited by: pete stein ]
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent, Thanks!

I used the code:

And this works if the JInternalPane has been added or removed.

I'm slightly new to Actions to, so I'll have to read up on it.
I've used ActionListener, and actionPerformed on buttons etc, but I haven't used Action or AbstractAction.

Thanks again for help, I should be able to work it out from here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic