I want to replace the text in my radio button list by an icon.
I've tried this:
rotateButton = new JRadioButton(rotateIcon.getImage());
But this replaces the radio button and text by the icon. I would like to keep the radio button and display the image.
What should I do?
What I'm currently getting is:
But I want it to end up with this:
Create a JRadioButton with no text and put a JLabel with the image next to it. You can also create a class to hide complexity.
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ChangeListener;
public class RadioButtonWithImage extends JPanel {
private JRadioButton radio = new JRadioButton();
private JLabel image;
public RadioButtonWithImage(Icon icon) {
image = new JLabel(icon);
add(radio);
add(image);
}
public void addToButtonGroup(ButtonGroup group) {
group.add(radio);
}
public void addActionListener(ActionListener listener) {
radio.addActionListener(listener);
}
public void addChangeListener(ChangeListener listener) {
radio.addChangeListener(listener);
}
public Icon getImage() {
return image.getIcon();
}
public void setImage(Icon icon) {
image.setIcon(icon);
}
} // end class RadioButtonWithImage
public JRadioButton(String text, Icon icon) and simple example here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With