• Post Reply Bookmark Topic Watch Topic
  • New Topic

error: method isSelected in class AbstractButton cannot be applied to given types;  RSS feed

 
Joshua Stephenson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program and I'm trying to add while using check boxes and I keep getting the error message error: method isSelected in class AbstractButton cannot be applied to given types;
What do I change so it works?

This is the portion of the program that doesn't work:

public void calc_actionPerformed(ActionEvent e) {
float totCost = 0.00f;
if (sml.isSelected(totCost + 5f));
if (med.isSelected(totCost + 7.50f));
if (lrg.isSelected(totCost + 10f));
if (hg.isSelected(totCost + 15f));
if (olv.isSelected(totCost + .25f))
if (pep.isSelected(totCost + .25f) );
if (oni.isSelected(totCost + .25f));
if (mush.isSelected(totCost + .25f));
if (sas.isSelected(totCost + .25f));


tot.setText("$" + totCost);
}
}
 
Campbell Ritchie
Marshal
Posts: 56201
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Are sml and med buttons? The names do not make it obvious. You need to work out what you are trying to do with those method calls. They obviously won't work as you have them there. Start by telling us what you are actually trying to do, and we'll see if we can't find something which does what you want. (Not me; I have to go elsewhere.)

I hope you have not been told to use floats for money. Any floating‑point arithmetic is wrong for money (this thread shows you what you should use), and floats are worse for imprecision than doubles.
 
Joshua Stephenson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell!

Sorry I should have gone into more detail.
What I am trying to do is have a GUI with several check boxes on it. Then when check boxes are selected and a button is pressed the selected check box's values will be added to make a total.
all the "if" statements are check boxes, sml, med, lrg, etc.
Also this is what it says the reason for error is: "actual and formal argument lists differ in length"
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message is telling you exactly what's wrong. You can't pass a float to isSelected. You should read the docs for that method to see what parameters it takes and what it does. Then you should ask yourself what you thought you meant by if some particular button . isSelected(some number)--that is why you wrote your code that way in the first place.
 
Joshua Stephenson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well what method would work for what i'm trying to do then?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't say not to use that method. I'm saying you need to study how it works and use it correctly.

You say you want to add something to a total based on what's selected. That's fine, but that doesn't mean that method knows anything about that. You can't just pass a number to a method that takes no arguments and expect it to magically know what you want it to do with that number.

You said:
Then when check boxes are selected and a button is pressed the selected check box's values will be added to make a total.
all the "if" statements are check boxes, sml, med, lrg, etc.


Before you try to write code, you should spell out that process in complete, precise detail in English and/or psuedocode, without regard to Java's syntax or API, but keeping in mind that if you do it right--that is if you're precise enough and your steps are simple enough--it will be easy to translate directly to Java.

 
Campbell Ritchie
Marshal
Posts: 56201
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that was probably what you wanted last night. I think you need to go back much farther than what you think. Why are you using check boxes? What do the individual boxes represent? How do you get $5.00 for a sml into your price?

Now start thinking object‑oriented. Which takes you even farther back than you thought I was leading you. How are you going to get $5.00 out of the sml object? Hint: the figure 5.00 must not appear twice in your code. Not even disguised as 5 or "5.00" with quotes.
Another thing: some of the code you showed has, “I have been using NetBeans,” written all over it. If you are still at the beginner's stage, you would probably do well to stop using IDEs and get a decent text editor, and follow the hints in this post.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!