action performed?

Hi experts.

ok I have a dektop in the center of the content pane and a panel on the east. then I have a method that will arrange the internal frames in the desktop a certain way.  now the method is working but I had to create a button and have this method in actionperformed resulting from pressing the button. I would like to have the method called when any component (whatever it is) is added onto the panel..how do I do that?
SamooramadAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
>>CEHJ, you mean use container listener and componentAdded() ?

Yes
0
 
CEHJCommented:
Add a ComponentListener
0
 
zzynxSoftware engineerCommented:
You mean you want to call a certain function whenever a component is added to the panel?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
zzynxSoftware engineerCommented:
Indeed, then a component listener could help.
But, aren't you adding the components yourself then?
Otherwise you could simply call your function after each add()
Or do I forget something?
0
 
SamooramadAuthor Commented:
>>But, aren't you adding the components yourself then

no not neccessarlly
0
 
SamooramadAuthor Commented:
which do I use ? component listener or container listener?
whats the difference?
0
 
SamooramadAuthor Commented:
CEHJ, you mean use container listener and componentAdded() ?
0
 
SamooramadAuthor Commented:
ok I'll try it
0
 
SamooramadAuthor Commented:
do I use
class myContainerListener extends adapter or listener?
0
 
CEHJCommented:
class MyContainerListener implements ContainerListener {
    // methods
}
0
 
zzynxConnect With a Mentor Software engineerCommented:
class myContainerListener extends ContainerAdapter {

    public void componentAdded(ContainerEvent e) {
    }
}
0
 
zzynxSoftware engineerCommented:
ContainerAdapter implements ContainerListener
An abstract adapter class for receiving container events.
The methods in this class are empty.
This class exists as convenience for creating listener objects.   // <<<<<<<< ;°)
0
 
SamooramadAuthor Commented:
this whole adapter, listener thing is still quite new to me zzynx.. I don't really get it or the whole implements concept :)
ok I'll see what I end up with
0
 
zzynxSoftware engineerCommented:
Well, it's not that difficult.

You should just have

1)
class MyContainerListener extends ContainerAdapter {

     private JPanel thePanel = null;

    public MyContainerListener(JPanel p) {
         super();
         thePanel = p;
    }

    public void componentAdded(ContainerEvent e) {
        // call the your method here.
        // Of course I don't know on which object it is called.
        // I supposed it is called on your panel. That's why I passed it as a parameter.
        // This way you can now write:
        thePanel.myMethod();
    }
}

2) somewhere in your code you have to make your listener listen:

    yourPanelOnTheEast.addContainerListener( new MyContainerListener(yourPanelOnTheEast) );

That's it ;°)
0
 
zzynxSoftware engineerCommented:
Thanks
0
 
CEHJCommented:
;-)

Since you only have a couple of methods in the interface, it's better to implement it than to extend an adapter, as you 'use up' your single inheritance
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.