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

swing actionListeners  RSS feed

 
auden james
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am a student working on a simple project, or so I thought. I have to design a calculator using Swing. I have the application set up, but I need help understanding actionEvents. My GUI looks good, all buttons have actionEvents attached to them, my LCD display works fine, but I have one major problem.

I am trying to set up my actionEvents like this.

I have one set declared within my Calculator class that simply adds a number to a string, so if the user pushes "1", then "2", then "7", my string will be 127.

I have another class that implements actionListener and is used for the plus, minus, divide, etc. buttons. This class saves the current string in the LCD display and then clears the LCD display.

I plan on using this structure, but don't know if it is a good setup or not. I would also like to know how I can pass strings between these classes. For example, when a user pushes the "+" button, the actionListener function that is called is outside of my Calculator Class. Therefore, I have to pass the LCDdisplay to this function. I would then like to save the LCDdisplay string, clear it, and pass back the what I saved. Can I do this. I can't find a way to pass this string back because all actionListener functions are void functions. Can I set it up so that when a button is pushed another function is called that is not a void function???

Any help would be greatly appreciated.

Thanks,
Cleatis
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it would be better for you to have all the events handled by one ActionListener (one class). Then you can build a String of each digit or operation without worrying about passing Strings around.

If you get this working you can always use the experience to try it the other way too.
 
auden james
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So in this actionListener class, how do I get the LCDdisplay(the string of numbers entered) passed to this class ??

I have no problem developing this application from a command line, but once I add swing buttons, I get confused.

Please let me know if the following is correct:
When a button is pushed, the actionListener is called. That function is declared something like public void actionListener(event e) (or something like that). How do I differentiate the events that occur when a number is pushed from the events that occur when either a multiply, or divide, or equals button is pushed. Are they all declared in the same function body??

Thanks again,
James
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, you're almost there. What you should do is add the same ActionListener to each button, something like this:

Then in your actionPerformed method, the ActionEvent object tells you all you need to know about the event that occured- including the object that caused it. So you can write your actionPerformed something like this:

Hopefully you get the general idea.

As for the LCDDisplay, make the main class (the main frame) the one that implements ActionListener. This class is where you are creating all of the JButtons etc. You can then treat the LCDDisplay just like any of the JButtons, i.e. after processing an ActionEvent you can do something like:

Hope this helps - feel free to ask again if you are still stuck.
 
auden james
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really appreciate the help. I have it working now.

Thanks again,
James
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!