• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

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?
0
Samooramad
Asked:
Samooramad
  • 6
  • 6
  • 6
2 Solutions
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
CEHJCommented:
>>CEHJ, you mean use container listener and componentAdded() ?

Yes
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
 
zzynxSoftware 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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now