Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

why this component access is multiplying?

 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic