1

I want to add a JTextfield to my JPanel. I am overriding the paint method and doing some drawing, because its a game.

If I am adding the JTextField it is not visible. If I know the position, I can click on it and I can even add some text, but the JTextField is flickering.

Is there any solution, that I can add a JTextField to my JPanel over the paint layer, so that is every time visible?

EDIT:

If you override paintComponent instead of paint, everything is working and you can simply add an JTextField. Thanks to trashgod.

SaintLike
  • 9,119
  • 11
  • 39
  • 69
Niklas
  • 23,674
  • 33
  • 131
  • 170
  • i dont understand, can't u just do `panel.add(textfield)`? and then use `textfield.setVisible(true);` – nachokk Jun 09 '13 at 00:30
  • No I can't. I am guessing that my paint method, which will draw over the whole screen override this textfield. – Niklas Jun 09 '13 at 00:37
  • 1
    "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://www.oracle.com/technetwork/java/painting-140037.html#callbacks). – trashgod Jun 09 '13 at 00:39

1 Answers1

3

Click anywhere on this AnimationTest to add a text field. Note the use of invokeLater() and the call to super.paintComponent(g). Resize the frame to see how the default layout works.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • if you still have problems, please edit your question to include an [sscce](http://sscce.org/) that exhibits what you encounter. – trashgod Jun 09 '13 at 00:41
  • Thanks, I am looking through the code and trying to understand, but this is the exact thing, I wanna to have. – Niklas Jun 09 '13 at 00:41
  • Take some time with it; don't hesitate to use it as a basis for any update. – trashgod Jun 09 '13 at 00:42