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
  • 8,381
  • 9
  • 37
  • 64
Niklas
  • 20,301
  • 31
  • 118
  • 158
  • 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
  • Thx trashgod, that solved my problem lol. – Niklas Jun 09 '13 at 00:43

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
  • 200,320
  • 28
  • 229
  • 974
  • 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