Click to See Complete Forum and Search --> : Keyboard focus?


Malone
10-04-2003, 12:42 PM
OK, Java gurus. I have a swing application, one JFrame with two JPanels. One JPanel is a sort of drawing area, the other has some JButtons and TextFields and stuff.

Now my problem is, I want to use the spacebar to do some things in the drawing area.

In the constructor of my JFrame I set up the JPanels and do this...

this.requestFocus(); // get keyboard focus
drawingArea.requestFocusInWindow(); // drawingArea is a JPanel

But when I hit the space bar, the only thing that happens is an ActionEvent is fired on the default button in the OTHER JPanel! :(

Obviously I am setting up the keyboard focus wrong somehow, but I can't find any decent tutorials on how to set the focus for a Panel (plenty of tut's for textboxes and such but none for panels). Any ideas?

Malone
10-04-2003, 06:15 PM
Nevermind, for those who are interested, one way to do it is to override the isFocusTraversable() method in your custom JPanel class to make it return true. Compiler tells me this is deprecated though, so I guess I still haven't found the "correct" way to do it.

rock
10-05-2003, 09:14 AM
FYI, we're a pretty good resource here at Sharky's. But if you run into tough Java issues or need another place to search for answers, check out Sun's Java forums (http://forums.java.sun.com/).