It's not a secret anymore!
The moose likes Swing / AWT / SWT and the fly likes Extending JPanel and Focus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Extending JPanel and Focus" Watch "Extending JPanel and Focus" New topic

Extending JPanel and Focus

Philip Dush

Joined: Feb 14, 2012
Posts: 1
So I've created a new class and called it gamePanel. It extends JPanel. I've added this panel to a frame, along with some buttons, in my main method. Essentially, by pressing buttons, it takes the focus off the panel which doesn't allow my key listeners (which were added to the panel) to work. Because gamePanel is a custom panel, and not a normal JPanel, using the method .requestFocusInWindow() doesn't work. so I made a method for my gamePanel object that when called upon will invoke the other method to give the focus back to the panel. However, it gives an error at the point in which my gamePanel object calls the method. So my ultimate question is, how can I get this working, or how can i return focus to my object?

If you need it, heres my code. Its long, and you should only need to look at lines 143 in "test" and line 21 in "gamePanel".

gamePanel class:

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Rob Spoor

Joined: Oct 27, 2005
Posts: 20188

To elaborate, key bindings.

How To Ask Questions How To Answer Questions
It is sorta covered in the JavaRanch Style Guide.
subject: Extending JPanel and Focus
It's not a secret anymore!