Hi,

I want to change the color of a selected jradiobutton in a button group to RED when clicked on it. I am not able to do it make it happen till now. The problem is actually that when I select any jradiobutton the selection spot should stay and get blocked at that particular radiobutton. Its not happening so I thought lemme try with color change atleast .Here is my ActionListener code:-

public void actionPerformed(ActionEvent e) {
                                if(e.getActionCommand().equals("myJRadioButton")){ 
       JRadioButton source = (JRadioButton)e.getSource();
       source.setFocusPainted(true);
       source.setEnabled(false);
       //ButtonModel model = source.getModel();
       //Group.setSelected(model, true);                                                           
}
}

So how do I set the focus only to the selected button and freeze it there or change the color of the selected button to show that it was selected by user ?

Thanks in advance

You could use something like this for your listener

ActionListener radioListener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        JRadioButton clickedButton = (JRadioButton)e.getSource();
        clickedButton.setBackground(Color.RED);
        if (clickedButton.isSelected()) {
            for (Enumeration<AbstractButton> buttons = bGroup.getElements(); buttons.hasMoreElements();) {
                AbstractButton b = buttons.nextElement();
                if (b != clickedButton) {
                    b.setEnabled(false);
                }
            }
        }
    }
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.