Disgruntled JTextField

I have added chat capabilities to Roll-A-Rama so you can press 'y' and it pops-up a JInternalFrame and gives focus to a JTextField in that frame so you can type your message that you want to send.  That works great.  However, after you send a message and the frame closes if you press 'y' again it all pops up exactly as it should the second time except the 'y' gets inserted into the text field (this happens every time except the first).  Any idea what would be causing this to happen?  The frame and text field are created dynamically after 'y' is pressed and destroyed after it's finished so the only conclusion I'm able to come up with is that the AWT thread is not running synchronously and is validating the 'y' key press late?



If you need some code reproducing this just let me know.



darkfrog

That behaviour sounds normal for me: The swing event caused by the keypress is processed after the component creation. 'Thread problems' if you want to call like that, yes.

As you should create components in the swing thread only, I'd suggest to react on the swing 'Y-pressed' event to show the text field (just as you would in a swing application). This way you'd avoid other problems with the components, too.

But will Swing pick up the key event if the JMEDesktop is not in focus?



darkfrog

yes, the event is triggered in jme thread (by lwjgl through input handler) and queued. And you can keep it focused if you like.

Btw. an invokeLater for your component creation should suite, too.

What would I add a KeyListener to though?  JMEDesktop receives every key event in focus or not?



The invokeLater is also a good idea, thanks!



darkfrog

To ease listening to awt keyboard events the desktop receives key events now if no component is focused.