Solved

action performed?

Posted on 2004-10-12
18
209 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

785 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