0

I'm having this problem where I can't figure out how to update the name of my JRadioButtons

I've set them up in the contsructor like so:

final JRadioButton ANSWER1 = new JRadioButton(answer1);

with answer1 being a String.

but whenever i change answer1, the name of the JRadioButton doesn't change.

I've set the JRadioButton to change name at the event of a JButton being clicked:

NEXT.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
    qnumber++;
    answer1 = "blah blah";
    ANSWER1.setText(answer1);

but this doesn't seem to have any effect, any help would be greatly appreciated, thanks.

1 Answers1

1
final JRadioButton ANSWER1 = new JRadioButton(answer1);

should be (search for Java naming conventions)

final JRadioButton answer1 = new JRadioButton(ANSWER1);

  • whatever hidden in String value answer1 (this variable should be defined as constant - private String ANSWER1) can be used for setName, setActionCommand, putClientProperty or descriptor for Swing Action, you can returns this value from every AWT/Swing Listeners added to JRadioButton

  • use JLabel for displaying narrative on the screen, you can to chains the JLabel with JRadioButton by using setLabelFor

  • use ItemListener for JRadioButton, test for SELECTED/DESELECTED instead of ActionListener
mKorbel
  • 109,525
  • 20
  • 134
  • 319