Set different text of JRadioButton when it is enabled and disabled

125 Views Asked by At

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.

2

There are 2 best solutions below

2
Chris On

Use SELECTED or DESELECTED status and do whatever there (ref)

  @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
            love.setText("Wow");
        }
        else if (e.getStateChange() == ItemEvent.DESELECTED) {
            love.setText("Loved");
        }
    }

Update:

Also, make sure to add a different ItemListener() for each of your radio buttons (don't use this for all of them), as below:

love.addItemListener(new ItemListener(){
    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
            System.out.println("wow");
        }
        else if (e.getStateChange() == ItemEvent.DESELECTED) {
            System.out.println("loved");
        }
    }
});
0
MadProgrammer On

You'll want to look at:

Runnable example

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            JRadioButton btn = new JRadioButton("Hello");
            btn.addPropertyChangeListener("enabled", new PropertyChangeListener() {
                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (btn.isEnabled()) {
                        btn.setText("Hello");
                    } else {
                        btn.setText("Good by");
                    }
                }
            });
            JButton toggle = new JButton("Toggle");
            toggle.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    btn.setEnabled(!btn.isEnabled());
                }
            });

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            add(btn, gbc);
            add(toggle, gbc);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.dispose();
        }

    }

}