1

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.

Sap Ashish
  • 19
  • 2
  • 1) [Edit] to add a [mre]. It should include enough code to get it on-screen without changes (e.g. add `import` statements and a `class` structure) but trim out anything not needed to demonstrate the problem (e.g. all the icons, fonts, and any more than *one* enabled / disabled control). 2) `radiobtn` should be `RadioBtn` for correct case usage for a class name. 3) `this.setSize(500, 300);` That's only a guess. For the correct size, add all components then call `this.pack();`. – Andrew Thompson Jan 14 '22 at 07:55
  • You'll want to look at [Laying Out Components Within a Container](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) and [How to Write a Property Change Listener](https://docs.oracle.com/javase/tutorial/uiswing/events/propertychangelistener.html) – MadProgrammer Jan 14 '22 at 07:59
  • .. 4) `if(ok=true)` should be `if(ok)` 5) Where is `love.setEnabled(false)` called? NVM I'll look for it in the MRE that will hopefully appear before 3 close votes have been added. – Andrew Thompson Jan 14 '22 at 08:01
  • 1
    "enabled" or "selected" - these are different concepts – MadProgrammer Jan 14 '22 at 08:06

2 Answers2

1

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");
        }
    }
});
Chris
  • 18,724
  • 6
  • 46
  • 80
  • *"I want to change text of the the JRadioButton when it is enabled or disabled"* - Your answer will track the selected state, not the enabled state ... but I'm taking the question literally – MadProgrammer Jan 14 '22 at 08:05
  • What their code is doing and what they want are always the same things ;) – MadProgrammer Jan 14 '22 at 12:10
0

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();
        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366