• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make my swing listen to key events?  RSS feed

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I am Chaitanya, I want to listen to key events on my JFrame. I have a JFrame and it consists a couple of internal frames. Each time i have to use the mouse to travel between the internal frames. So i decided to add a key listener to the JFrame and hear to F5 F6 F7 function keys. Pressing those keys will focus an internal window. Thats my idea.

I added my JFrame to a DesktopPane and added key listener to desktopPane, it dint work. Then I tried with JFrame object. That also dint work.

Can anyone tell me how to do this? Thanks all in advance.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Key events are working when there are no internal frames. How to do it when there are internal frames?
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read in this post that we cant register keyboard events with JFrame and JInternalFrame combination.

Is there another alternative?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chaitanya karthikk wrote:Is there another alternative?


Yes, there is. Read up on Key Bindings.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the ticket. The problem with using a KeyListener here is one of focus.
You can use key bindings to add function key bindings to the correct input map (there are three) of the JDesktopPane.
Then you can bind the action key to a javax.swing.Action using the JDesktopPane's action map.
That action can then toggle the desired JInternalFrame via its setSelected() method.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!