2

I am creating a Java applet, and I am trying to add a JFrame to it. I am calling:

add(new MyJFrameSubclass());

But when I launch the application, my JFrame isn't shown until I resize the window.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
  • Are you creting an Applet to embed in a webpage? Or are you trying to create a standalone application? – jjnguy Aug 14 '10 at 22:33
  • I am trying to create an application that functions as both - so you can run the jar locally, or use it as an applet. – Jamie Allfrey Aug 14 '10 at 22:34

3 Answers3

2

This may be too elementary of a suggestion, but sticking in a validate() or repaint() can sometimes solve problems that seem complicated.

nova
  • 205
  • 1
  • 9
2

A simple fix is to add a frame.show(); after you add your JFrame, I had the same issue and this seemed to help, the frame being the name of your JFrame.

Marcus
  • 12,296
  • 5
  • 48
  • 66
nobody
  • 21
  • 1
1

JFrame and JApplet are both top-level containers. Instead of trying to have both containers present at once, put your content in a lightweight container such as JPanel; then add that JPanel to either a JFrame (for local use) or a JApplet (for the browser). This example shows the approach.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045