Locking when using JTextField

Hi,
I’m using JTextField in my game to receive user input.
Problem is the game get stuck when i use the text field.
I tried to override the setText method and lock the text field before setting its text but it didn’t work.

Due to time constraints i don’t want to create my own customized text field.
Any ideas on how can i prevent the locking?

Thanks,
Jo

I feel you will need to elaborate on what ‘locking’ means in your case and about the environment the JTextField gets to live in.

By locking i mean that the game simply get stuck…
It seems that there is some sync isues with the event dispatcher thread.
I’m using active rendering in my game and the game hangs when the text field has the focus and i call the layered pane’s paintComponent method (not always but after few calls to the method).

Hard to tell what the problem might be, with so little information given.

Here are a few thoughts:

  • Check whether you manipulate any Swing components outside of the AWT thread. The spec does explicitly not allow this, and it can actually lead to severe and rather unpredictable problems.

  • Have you customized the JTextField (i.e. subclassed it, changed the look and feel)?

  • Do you have any listeners registered which may get stuck?

  • If possible, try to dig as deep as possible into the stack trace to see where the AWT thread hangs. Debugging into the Swing/ AWT classes works quite well with Eclipse, and usually gives you at least an indication what the problem may be (e.g. a lock on the component tree).

Let me clear some things:
I’m using JFrame to render all game objects (bg image, game objects, huds etc.). For that i’m not using any swing components (except the JFrame itself).
At the end of the game i’m using JPanel as high scores menu. The panel uses text field for the name and two buttons.
When i try to enter text in the text field the game hangs after few seconds (with no exception but i can point the line it happens in the code).

The rendering is quite simple and looks something like this (gameFrame is the JFrame):

gameObjects.render(g);
gameFrame.getLayeredPane().paintComponents(g);

When the game hangs it always happens in the second line above.

I’m using some other menus with buttons only and they never cause any problems.

At first i used regular JTextField and when i realized it causes problems i subclassed it and synchronized the setText method (…synchronized (getTreeLock()) but it didn’t work either.

I managed to prevent the locking by using EventQueue.invokeAndWait to call gameFrame.getLayeredPane().paintComponents(g) but i don’t like this “solution” because it forces me to wait and use another thread.

From where do you call the game rendering code? I assume that you call it from a game loop running in a separate thread.

If that is the case, the hanging is probably explainable. The paint() method of JComponent (which in turn calls the paintBorder(), paintComponent(), and paintChildren() methods) is not supposed to be called from any other but the AWT thread.

In general, Swing is not a multi-threaded framework, and many things can go wrong if you treat it as such.

If the above scenario applies, using invokeandWait() is valid and actually recommended. In that case, however, I would consider to simply use repaint() in your game loop, and implement the paint() method of the JFrame.