0

Some background: I have a turn based game where you can play several concurrent games in separate windows. Each window/game has it's own chat and also a kind of game action panel that turns up when it's your turn to act.

The issue: Game windows will steal focus whenever it becomes your turn to act in any of the games you're playing, this is by design but very annoying if you're involved in chatting at any of your games because when focus is lost that chat will no longer receive your key board strokes.

What I want: Some way to dispatch key board events to a JTextField that's no longer the focus owner (and also in a different window/JFrame than the current focus owner). Is there some way to do this? And how?

Plan b would be to set some kind of timer on the chat and let the window refuse to give up focus until x amount of time has passed since the last key stroke in the chat, but it might not be ok to stall the focus switch since your action time is limited already.

Oskar Lund
  • 339
  • 2
  • 11
  • Your description seems like an 'unworkable GUI'. I pity the users who are forced to deal with such a mess. – Andrew Thompson Feb 15 '11 at 02:17
  • Dude.. care to elaborate on that? I've naturally not given all details about the app, what if you misunderstood something? This is not the place for poorly founded personal opinions on other peoples work or ideas. If you don't have anything constructive to say then keep it shut. I'm really surprised to see a comment like yours from someone with your level of rep on here. Thats the real pity in all this. – Oskar Lund Feb 17 '11 at 10:18

2 Answers2

0

You can set alwaysOnTop of the current chat window true while there's text in the chat text field by calling:

java.awt.Window
public final void setAlwaysOnTop(boolean alwaysOnTop)
                         throws SecurityException

When user presses Enter or clicks on Send button reset the alwaysOnTop so other windows could steel the focus.

Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
  • Using alwaysOnTop is not an acceptable solution I'm afraid, I have to keep letting windows move to front when it's your turn to act or it will mess up the game play. User doesn't have that much time to act so he needs to be presented with the window where he needs to act as soon as it's his turn. Maybe 1-2 secs of stalling the focus switch would be ok but I'd rather try and keep dispatching key strokes to an unfocused chat if possible. – Oskar Lund Feb 14 '11 at 16:29
0

I might have found what I was looking for here. Remains to be seen if I'll try to use it or if we'll change the requirements :)

See this link to find a working SSCCE on how to redispatch KeyEvents to any text component.

Community
  • 1
  • 1
Oskar Lund
  • 339
  • 2
  • 11