Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to add a Panel to my applet?

 
shalaka wadekar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a java class in which I constructed an applet. This file is compiled & when executed displays a blank applet.
Now I have another java class in which I am contructing a JPanel. I want to add this JPanel to my applet for which I use this code:


The last line add(content); gives a compilation error. It says "cannot resolve symbol, symbol: method add()"
Can anyone please tell me whats going wrong?? And what can I do instead?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whichever class you are in does not have a suitable add() method.

You probably need something like myApplet.getContentPane().add() or getContentPane().add().
 
Rob Spoor
Sheriff
Pie
Posts: 20555
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Applet is an indirect subclass of Container, so the method should be there. Does your class extend Applet or JApplet?

If it is a JApplet you want to use getContentPane() like Campbell suggested, or you will get a runtime exception when used in Java 1.4 or before. Only since Java 5 does JApplet (and JFrame and JDialog) redirect the container specific methods (adding, removing, layouts) to its content pane.
 
shalaka wadekar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


Thankyou for responding. My class is extending JApplet. I am using java1.4 so that shouldnt be a problem. Where should I use the getcontentpane() method & how. How do I use it to send the content of my panel to the Applet.

Regards,
Shalaka
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a java class in which I constructed an applet. This file is compiled & when executed displays a blank applet.

This sounds suspicious. How do you "construct" an applet? Applets should not have a constructor (besides the default no-arg constructor), nor should code of yours ever call it. That's for the browser JVM to do.

If this is not an applet that's intended to run in a web browser, then you're asking for trouble subclassing Applet or JApplet.
 
Rob Spoor
Sheriff
Pie
Posts: 20555
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shalaka wadekar:
My class is extending JApplet.

Can you show us part of the code then? Especially the part where you add components to the applet.

I am using java1.4 so that shouldnt be a problem. Where should I use the getcontentpane() method & how. How do I use it to send the content of my panel to the Applet.

Since you're using Java 1.4 you should use getContentPane() like I said. You'll need to do this at every point where you want to add a component directly to your applet.
 
shalaka wadekar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you!!

This one got resolved, I used JTabbedPane instead.

Regards,
Shalaka
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic