Solved

action performed?

Posted on 2004-10-12
18
188 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
What is JNDI datasource in spring 1 39
wordcount challenge 11 78
Eclipse Neon and jdk 1.8.0 11 72
Java and GPO 11 47
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now