I'm making a project in jFrame. It has 15 jRadiobuttons. What I want is a jButton to add the selected jRadioButtons and display the sum in a jTextField. Can anyone help me? I've been doing this for 3 days and cant get it right.
Asked
Active
Viewed 179 times
1 Answers
0
So you have numbers as values of the radiobuttons? Then try it like this: When the button is clicked, go through a foreach loop of all radiobuttons. in the loop, check if they are selected. If they are, you add their value to the Sum.
Is that what you were asking for?
EDIT
- You create A List containing all radiobuttons.
In the Listener of the Button you write:
int sum = 0; //foreach loop for (jRadiobutton rbutton : rbuttons){ //Check if the button is selected if (rbutton.isSelected){ sum += rbutton.Value; } }
I hope this is good enough explained. Maybe the Syntax isn't correct, i haven't used java for a while now...

M.Zuberbühler
- 187
- 10
-
Yes sir, all the radiobuttons have number values. What i want is for me to be able to pick any radiobuttons and when i click the jbutton it sums the total amount. Im having trouble w/ that one. – Chester Vacalares Oct 07 '15 at 11:29
-
@ChesterVacalares Have you tried it the way I wrote in the answer? – M.Zuberbühler Oct 07 '15 at 11:32
-
Not really. Can you instruct me in layman terms. Im actually new w/ this. – Chester Vacalares Oct 07 '15 at 11:34
-
@ChesterVacalares Answer is edited. Is it clear now and if yes, does it work? – M.Zuberbühler Oct 07 '15 at 11:44
-
do i have to do this in every radio button? – Chester Vacalares Oct 07 '15 at 11:54
-
@ChesterVacalares You have to do this in your Button to calculate the sum. (jButton in your Question) – M.Zuberbühler Oct 07 '15 at 11:56
-
im getting errors in jRadioButton, rbuttons and if(rbutton.isSelected). Am i missing something? – Chester Vacalares Oct 07 '15 at 12:04
-
@ChesterVacalares Like I said, the syntax may be wrong... after isSelected put () and rbuttons would be the List you have to create. – M.Zuberbühler Oct 07 '15 at 12:31
-
i get the JRadioButton now. Whats left is the rbuttons and value that is still error. – Chester Vacalares Oct 07 '15 at 12:32
-
@ChesterVacalares maybe .Value doesn't exist. is there something like getValue() or getText()? And for rbuttons you have to put List
rbuttons = new ArrayList – M.Zuberbühler Oct 07 '15 at 12:36() at the beginning of your code and add the radio buttons to it -
can you show me an example.? I need this for tomorrow. – Chester Vacalares Oct 07 '15 at 12:41
-
@ChesterVacalares sorry i have no java environment. Can you somehow upload somewhere your Code and i can look at it? – M.Zuberbühler Oct 07 '15 at 12:43
-
got it... Thank you so much – Chester Vacalares Oct 07 '15 at 12:53
-
is livenet russian sir? i cant really register. Do you have gmail or yahoo? – Chester Vacalares Oct 07 '15 at 13:30
-
@ChesterVacalares Sent you the Code. And no, livenet is from Switzerland. And you don't have to register... But it worked so what – M.Zuberbühler Oct 07 '15 at 14:00
-
total += (int) button.getText(); is in error. It says "incompatible types: String can not be converted to int." – Chester Vacalares Oct 07 '15 at 14:18
-
@ChesterVacalares I sent you a solution for that in a Mail. Is it still showing the error? – M.Zuberbühler Oct 07 '15 at 14:19
-
I've sent you an email. Just a heads up incase you missed it – Chester Vacalares Oct 11 '15 at 02:08