Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Are ActionListeners and ActionCommands Needed to Make This Simple Calculator?  RSS feed

 
Zachary House
Greenhorn
Posts: 11
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So please pardon the very, very messy code. I am basically just starting Java coding. However, I have a decent amount of past coding experience.

I was wondering if there is a much easier to assign the ActionListeners and ActionCommands without writing everything out? Better yet are they even needed?

The code below is only the exact spot I have started at with my basic calculator: coding the number keys. I want each number to appear on the calculator
"screen," which is just a label, just as they would on a regular calculator. That includes sliding over any pre-existing numbers in on "screen" and tacking on
the number you just pressed. I thought about assigning each button a string variable, with it's value equal to the number it represents.

I have already tried searching for a topic like this in several forums. I believe someone might have hinted at using the Lambda (λ) Symbol, but that was in
response to an entirely different issue, and did not explain how to implement it.

 
Zachary House
Greenhorn
Posts: 11
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Proofreading my post AFTERWARDS has proved to be a big mistake.

Corrections: removed the JButton 10, fixed all the assigned values under public class CalcInProgress {

My apologies for any confusion caused.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
without writing everything out

One approach  is to use an array to hold the references and a loop to fill the array.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I personally think addActionListener(this) is non‑object‑oriented code, and shou‍ld be avoided like the plague. What you get is a great big block of repeated if‑elses, which look even worse for calculators than for some things. You get horrors like this:-Even worse, each button already has its number already:-
So that means you can use the information in the button already, even if only as its text. Yes, you usually pass a String to a button constructor, and in a calculator, that would usually look the same as the number it represents: "3" ≡ 3.

Norm is right that you can create an array of buttons and iterate it. You can create an anonymous Listener for each button (yes, I think you do need action listeners, but you probably don't need action commands):-You shouldn't find it hard to translate that code to use an array, creating the buttons and adding all their details as you iterate the array.

Now, that is how we had to do it in JDK1.2 to Java7, but now that Java8 is here and ActionListener is a FunctionalInterface, you can shorten that to a λ. Let's start by reducing my code to option 2 only:-Now, the javac tool “knows” that this method can only take one type of argument, so you can delete the name of the class and new and some miscellaneous brackets:-So far, so good, but as you will see, ActionListener has one method and only one method, so the javac tool can predict the method name, so I can delete that, along with some more brackets:-Now you need to join the parameters to the method body with the -> arrow token, which I usually read as, “goes to”.That will work, but you can simplify that even more; there is only one version of the method, which isn't overloaded, so it can only take one type of parameter, so you can (as often) get away with omitting its type:-And one last refinement: because you have exactly one token in the () to the left of -> you can omit those ():-As Norm has already suggested, you can do all that inside a loop to create your nine or ten numbered buttons, using the loop index to give you the number. A bit of jiggery‑pokery with the / and % operators can even supply the locations to add them to a panel.
Even without having a class for each button (as might have happened with anonymous classes), you have an action listener object attached to each button, so that is a much more object‑oriented solution. Now you simply need to write the method for the arithmetic.

Please search my posts in this forum, this other forum and this one, because there have been some questions very similar to your in the last two or three weeks.
Please check how code tags are supposed to work. Thank you for using them. I corrected the tags, and doesn't your code look better now
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering if there is a much easier to assign the ActionListeners and ActionCommands without writing everything out?


Check out: https://coderanch.com/t/630056/java/Adding-ActionListener-JButtons#2884374.

It demonstrates how to share an ActionListener for all buttons with the same functionality. In this case the text of the button is simply appended to the calculator display.
 
Zachary House
Greenhorn
Posts: 11
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To all the people that provided some feedback, primarily Ritchie, thank you for the help. This is all very usable information, provided (for on of the first times) in a way that I can understand, as opposed to the usual expert coder jargon. I apologize for not having found those posts similar to mine in the past few weeks. I will try doubly as hard next time to search for an existing thread whenever I have a question.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nor can I find the old threads. Sorry.

And . . . “that's a pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!