I want to change text of the the JRadioButton when it is enabled or disabled. If the button is disabled it should should shows one text and when it is disabled it should show another text. Somehow I managed to make it change text when it is enabled. But my problem is that even when the button is disabled, it shows the text when was enabled. I tried using JCheckBox instead of JRadioButton it the problem wasn't fixed. Here is the the code;
'''
radiobtn() {
this.setSize(500, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setBackground(new Color(0x00CCFFAAFF));
this.setTitle("Radio Button example");
this.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 52));
loveIcon = new ImageIcon("radio_icon\\icon (1).png");
sadIcon = new ImageIcon("radio_icon\\icon (2).png");
angryIcon = new ImageIcon("radio_icon\\icon (4).png");
wowIcon = new ImageIcon("radio_icon\\icon (3).png");
hahaIcon = new ImageIcon("radio_icon\\icon (6).png");
careIcon = new ImageIcon("radio_icon\\icon (5).png");
love = new JRadioButton("Loved");
love.setBackground(null);
love.setFocusable(false);
love.setForeground(new Color(0x00000));
love.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
love.setIcon(loveIcon);
love.setSelectedIcon(wowIcon);
love.addItemListener(this);
sad = new JRadioButton("Sad");
sad.setBackground(null);
sad.setFocusable(false);
sad.setForeground(new Color(0x00000));
sad.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
sad.setIcon(sadIcon);
sad.setSelectedIcon(hahaIcon);
sad.addItemListener(this);
angry = new JRadioButton("Angry");
angry.setBackground(null);
angry.setFocusable(false);
angry.setForeground(new Color(0x00000));
angry.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
angry.setIcon(angryIcon);
angry.setSelectedIcon(careIcon);
angry.addItemListener(this);
this.add(love);
this.add(angry);
this.add(sad);
this.setVisible(true);
}
public static void main(String[] args) {
new radiobtn();
}
@Override
public void itemStateChanged(ItemEvent e) {
boolean ok = e.getStateChange()==ItemEvent.SELECTED;
System.out.println(ok);
if(ok=true)
love.setText("Wow");
}
}
'''
in the above code, I want to change the text of love button from Loved to Wow when the button is enabled and again when the button disabled I want to change to Loved from Wow. When I click on that button, it does change the text to Wow but again when I click it to disable it, the text is not changed back to it's original text i.e. Loved. It is set Wow.