Solved

action performed?

Posted on 2004-10-12
18
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 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.

717 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