Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JButton ActionListener  RSS feed

 
dushantha Rathnayake
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am developing an application. I am using one JFrame and others are JInternalFrames. But the problem is, if I click a button in JInternalFrame at first time it is work properly. If I close that InternalFrame and open it and click that button again, it is do its action two times.what I want to do now? please help me.

Thank you.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code has a bug in it. but we can only make guesses as to what the problem is without code. So for more precise help, you will need to post some code. My guess is that you may be using static variables (such as a static JButton) that accumulate actionlisteners, but again this is nothing but a SWAG.
 
dushantha Rathnayake
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


hi,

try with "Edit --> Add Program --> give a number for Number of Applications and press start" after that another internal frame will be open. then you have to give name of application and add it (select it). but the next time I cant give name of application.because "add" button automatically clicking.please help me.

thank you. ulate actionlisteners, but again this is nothing but a SWAG.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every time addProgram() is called, you add another ActionListener to the addPStart JButton, and so this JButton will accumulate listeners, and all of the repetitive listeners will fire when the button is pressed. Solution: don't do this, but instead perhaps declare and create your dialog when needed, or create it once and don't recreate it.

More importantly, you have a huge single god class here with a switchboard action listener, two anti-patterns that need to be corrected so that your code is understandable and debuggable for us and for you. Some intensive refactoring can help here.
 
dushantha Rathnayake
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thank you Pete stein. Actually I want recall to that addProgram window. Can I erase previous actionlistenr? If I erase it then I can call again know? is there any method for do that?

Thank you.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dushantha Rathnayake wrote:
Thank you Pete stein. Actually I want recall to that addProgram window. Can I erase previous actionlistenr? If I erase it then I can call again know? is there any method for do that?


I see a couple of possible solutions (and there are likely more). You could:
  • declare, initialize, and show the addPFrame JInternaFrame object from within your addProgram method or
  • create the addPFrame JInternaFrame object once on program start up and re-use it without repopulating it from within the addProgram method or
  • create the addPFrame JInternaFrame object using lazy initialization from within the addProgram method (if addPFrame != null) {... create it}


  • What I wouldn't do would be to remove and add ActionListeners as that's a bad kludge to try to fix a program design problem (in this case as sometimes you may want to do this, ... just not here!) -- better is to improve the program design.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!