Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

multiple action listeners ?  RSS feed

 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So im making a calculator just basic 4 function with a clear button. as i am working into this project i am running into an issue of assigning which click sets what. For instance i click the two button how do i tell it to assign it to firstNum, versus secondNum? I am not posting all of my code im going piece by piece and i cant seem to understand how to get past this part.


So this for instance allows me to set one to the firstNum and then add it to two or three but im not sure what to do next. I have in my mind i want to do something like make three listener classes, that operate in sequential order like you pick the first numbers, click an operation, and then select the second numbers then hit the operation(equal) button to display answer. Am i on the right track or what?
 
K. Tsang
Bartender
Posts: 3624
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing to look out for is what is event.getSource()? Does this return the (add, subtract etc) button or something else.

In terms of storing the numbers, if the action listener can access the variables from the GUI code, then it will be fine.

I suggest you work on say addition first and get that sorted before moving forward.

If I were doing it I will do something along like this:



Note: I have not try compiling/running the above code.


Each button should have it's own listener.
 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh my gosh thank you for posting the code

This is what i couldnt wrap my head around. That when i clicked a button ,which is what getsource did, that it should send that number to the display screen to be pulled off as the whole number. I was trying to set a number to a variable each time it was clicked. I do have another question my code looks not pretty to me with a lot of if statements around the buttons.

Is this okay? My teacher used to mark me off when i used a lot of if statements but i think that was more because i used nested ifs in ifs. thank you so much by the way
 
K. Tsang
Bartender
Posts: 3624
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad that my code helped.

Again ask yourself what is event.getSource()? Are you going to use the same listener for all buttons?

What exactly is "one", "two" etc?

I suggest you separate out the listeners to make life simple. Hint: Look again at my listeners, I didn't use event.getSource(), but have a method called sum().
 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a method called intSum lol. i have more code i am not posting. the get source is the way i am reading in the number, then once plus is clicked it will store the display screen as firstnumber and then read in the second number from the display screen, the method intSum will be called once equals is clicked and sent to display screen. i am using getsource to read in the clicks on the numbers. how else should i go about that? and why would i want/need a different listener for "clicks" on buttons, each button will trigger something once it is clicked.
 
K. Tsang
Bartender
Posts: 3624
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. Not knowing how you set up your GUI, I thought you would have one button for add, one for subtract, one for multiply and one for divide. That's four at least plus the equals.

The event.getSource() should return the button, not the text field. So based on your approach, it may not be what you expect.

If you are using one listener then the event.getSource() would be needed to determine which button is clicked. Honestly I wouldn't recommend you using the instance to check which button like what you had but check the text (or some other property) of the button
 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it does return the button sorry, one is the name of my button "1" it is a full calculator with all the number buttons etc, like a simple windows calculator
 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is my view class right now, i also have a model which had my methods for adding etc, and the one to run it with the main
as you can see i am in the process of changing stuff still
 
K. Tsang
Bartender
Posts: 3624
16
Firefox Browser Java Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting.

From the look of things, your GUI code looks alright. All you need to work on is the action listeners.

Here is what I suggest:
The buttons 0 to 9 and decimal can have the same listener, this ultimately update to your solutionField for display once clicked (taking out the actual calc)
The +,-,*,/ buttons each have their own listener, hence calling the appropriate method that does the calculation, the calculated intermediate result display to solutionField
The clear button its own listener, empty the solutionField
The = button its own listener, setting the final result to some value, the solutionField doesn't need updating but if wanted update it with the final result

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!