wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JRadiobutton problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JRadiobutton problem" Watch "JRadiobutton problem" New topic
Author

JRadiobutton problem

Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
I created 2 radiobuttons, put it into a group box, and gave one of them a function called deposit ( a simple maths calculation) and the other, withdrawal.However, when i run the program, even when i don't select the 1st button, it automatically runs the deposit function, and when i click the other button, it doesn't execute the withdrawal function. I''m guessing that its either:

-the true value at the radiobutton has a problem
-the boolean b part is assigned wrongly
-or I'm using the isSelected function the wrong way

Can someone please take a look at it and see if where i should fix? Alternative suggestions are extremely welcomed too!



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

1) A button group always has a selected item. If you don't select one, the first will be selected.
If you don't want any selection you should include an extra JRadioButton which you add to the button group but do not show in the GUI. By selecting this radio button (through code) you can also remove the selection of the other radio buttons.

2) Your code is never triggered by events. You should add an ActionListener (or Action) to each JRadioButton, and add your JOptionPane code to that (those) listener(s) / action(s).

You should also check out http://java.sun.com/docs/books/tutorial/uiswing/components/button.html


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
i've tried using ActionListener for the JRadioButtons, but, the value that I would like to call in the function (currentbal) can't be retrieved and I'm not sure how to actually implement an ActionListener inside an ActionPerformed method. A bigger part of the code is as below, just to help you guys get a clearer picture.




I would like to know, if there is any way, I can fix this code to set a String to represent the JRadioButton, so that i can replace it as something like this :

if(jrbtn1="deposit"){
}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

You can use the action command. But if you do, please don't use ==, use the equals method instead.
Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
Thanks Rob, I'm sorry as I still couldn't get it. I wonder if anyone could give me an example on how to do it, or direct me to some similar cases or demonstration. I'm confused
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

In the actionPerformed method for the ActionListeners for those buttons:
As for creating the ActionListener for these buttons, you can go a few ways:
1) create an inner class that implements ActionListener, instantiate that and add it
2) use an anonymous class
3) if your class itself implements ActionListener, use "this". Using the code I've shown you before you can figure out which button was pressed.

And you might want to read http://java.sun.com/docs/books/tutorial/uiswing/components/button.html
 
 
subject: JRadiobutton problem