Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

JRadiobutton problem  RSS feed

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!



 
Sheriff
Posts: 21450
95
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Luen Hong Wuan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21450
95
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the action command. But if you do, please don't use ==, use the equals method instead.
 
Luen Hong Wuan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21450
95
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!