aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes why this component access is multiplying? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "why this component access is multiplying?" Watch "why this component access is multiplying?" New topic
Author

why this component access is multiplying?

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Hello,
I have made the followin program and facing this problem. Whenever I click at go of Login screen it adds while printing on system. What i mean if i enter data into id & password and click at go it shows its value first time one time on scrren and in second tme 2 times and third times 3 times..and goes on adding up..
can some body help me WHY?
reagrds,
arun
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Arun,
Your problem stems from the fact that you are recreating the login panel for each call to your login method. But you are not changing the go button. Since your go button is a instance variable you will always have it and every time through your login method you add another listener to the go button.
What you probably want to do is to recreate the go button every time inside the login method. Then you will not be adding more listeners but only one.
Split the definition and initialization of the go button:

Then all will work as you expect.
Regards,
Manfred.
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Thanks a lot for your reply. It works very well.
But after reading your answer it seems this program can be written in better way. Can you spare a moment and let me know how to write in better fashion to get more efficency from this program.
regards,
Arun
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
Hi Arun,
Your makePanel method was creating the problem, dont call mp = new ..in ur main class.., first of all declare ur memmer variables in main class, then in ur test constructor call methods which will create define the members & create the panels..
test
{
JLabel j;
public test()
{
buildToolbarPanel();
buildMainPanel();
.
.
}
buildMainPanel()
{
j = new JLabel();
....
}
.
.
}
also try not to create a monolithic code for ur actionPerformed(), try as far as possible to use adapter or inner anonymous classes & in each u can call methods so they can be reused, for eg.
login.addActionListener( new ActionListener()
{
public void actionPerfomed(ActionEvent ev)
{
doAction();//u could reuse this doAction
}
});
try to avoid creating unnecessary objects..thats the most common of mistakes as u learn,..
& u will learn more & more tricks by reading Javaranch's code snippets


- Vinod<br />-------<br />SCJP2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why this component access is multiplying?