开发者

Containers class owner's child object having to add components into his parent's container problem

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.


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)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜