• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to add a Panel to my applet?

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all of you!!

This one got resolved, I used JTabbedPane instead.

Regards,
Shalaka
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic