views:

56

answers:

2

Hi,

I am having problems dealing with containers and components in my JApplet. I don't put my code because I think there is no need for it.

The main class owns the getContentPane().

Another class (let's call it class1) owns a JLayeredPane() and components that are added to it.

Another class (let's call it class2) only have components to show.

At the init(), the main class adds class1's JLayeredPane().

At a later time, class1 adds its components to its JLayeredPane(), and create a new object from class2.

Class2 is supposed to generate a variable number of components (the number of components and their properties change with time), but can't add them to class1's JLayeredPane().

How can I have the class2 components to be showed ?

Thanks for reading.

+2  A: 

Three ways
1. Pass JLayeredPane() to the class2's constructor or a method
2. Class2 has methods that return the components that Class1 can
   add to JLayeredPane()
3. Pass Class1's object to class2 which will call Class1's method through
   a known interface that Class1 implements (a callback)

Murali VP
The first way worked perfectly !Thank you.
Cheshire
+1  A: 

You can:

Give a reference of class1 to class2

....In Class1 code
Class2 two = new Class2();
two.setClass1Ref( this );

And each time two adds new components set them to one

... in Class2 code 


 Class1 one ...

 JComponent newComponent = .... 

 one.add( newComponent ); // onw.add delegates to its own JLayaredPane

If you don't want to have a reference of Class1 in Class2 then you can add a callback method on or something like a ContainerListener

OscarRyz