aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to add a Panel to my applet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to add a Panel to my applet?" Watch "How to add a Panel to my applet?" New topic
Author

How to add a Panel to my applet?

shalaka wadekar
Ranch Hand

Joined: Mar 21, 2008
Posts: 37
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?


shalaka
SCJP
SCWCD
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
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

Joined: Oct 27, 2005
Posts: 19792
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
shalaka wadekar
Ranch Hand

Joined: Mar 21, 2008
Posts: 37
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
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  70
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

Joined: Oct 27, 2005
Posts: 19792
    
  20

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

Joined: Mar 21, 2008
Posts: 37
Thanks to all of you!!

This one got resolved, I used JTabbedPane instead.

Regards,
Shalaka
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add a Panel to my applet?