Dannon
Dannon

Reputation: 2216

Focusing in different JPanels? (java)

Quick question, I have 3 JPanels, one has a keylistener and it draw my game graphics, and you can alter it by moving your character around, etc. and I have a second JPanel which is just a JTextArea, and it just displays text, and finally I have another JPanel which has a JTextArea and a JButton, the text area takes inputs and you may hit enter key or click the button to send the text which will appear in the other text box. But my question is, how can I request focus in each panel? How do i transfer the focus with lets say a mouse click?

I know that if I dont constantly call requestFocus(true); for the first JPanel with displays all the graphics it wont work for some reason. I believe its because its always painting and maybe it looses focus on every go around. But how to I in all get the focus to transfer with a mouse click, etc?

Upvotes: 0

Views: 129

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

One possible solution is to not use a KeyListener. Instead use Key Bindings which are much more forgiving with regard to focus. Have a look at the Key Bindings tutorials which Google can help you find in a jiffy.

For more specific help, consider creating and posting an sscce.

Upvotes: 4

Related Questions