Solved

action performed?

Posted on 2004-10-12
18
215 Views
Last Modified: 2010-03-31
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
Comment
Question by:Samooramad
  • 6
  • 6
  • 6
18 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12290682
Add a ComponentListener
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12290685
You mean you want to call a certain function whenever a component is added to the panel?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12290702
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 37

Expert Comment

by:zzynx
ID: 12290707
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12290719
0
 

Author Comment

by:Samooramad
ID: 12291488
>>But, aren't you adding the components yourself then

no not neccessarlly
0
 

Author Comment

by:Samooramad
ID: 12291518
which do I use ? component listener or container listener?
whats the difference?
0
 

Author Comment

by:Samooramad
ID: 12291532
CEHJ, you mean use container listener and componentAdded() ?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 150 total points
ID: 12291574
>>CEHJ, you mean use container listener and componentAdded() ?

Yes
0
 

Author Comment

by:Samooramad
ID: 12291822
ok I'll try it
0
 

Author Comment

by:Samooramad
ID: 12291915
do I use
class myContainerListener extends adapter or listener?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12292036
class MyContainerListener implements ContainerListener {
    // methods
}
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 100 total points
ID: 12292041
class myContainerListener extends ContainerAdapter {

    public void componentAdded(ContainerEvent e) {
    }
}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12292049
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
 

Author Comment

by:Samooramad
ID: 12292116
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12292258
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12292260
Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12292264
;-)

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question