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!
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).
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 :
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.