JRadioButton: how to replace text by IconImage?

前端 未结 4 1670
不思量自难忘°
不思量自难忘° 2021-01-13 10:22

I want to replace the text in my radio button list by an icon.

I\'ve tried this:

rotateButton = new JRadioButton(rotateIcon.getImage());
相关标签:
4条回答
  • 2021-01-13 10:41

    public JRadioButton(String text, Icon icon) and simple example here

    0 讨论(0)
  • 2021-01-13 10:50

    I just reproduced your described behavior using this source:

    import java.awt.Image;
    import javax.swing.*;
    import javax.imageio.ImageIO;
    import java.net.URL;
    
    class RadioWithImage {
    
        public static void main(String[] args) throws Exception {
            URL url = new URL("http://www.gravatar.com/avatar/" +
                "a1ab0af4997654345d7a949877f8037e?s=128");
            Image image = ImageIO.read(url);
            final ImageIcon imageIcon = new ImageIcon(image);
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JRadioButton radioButton = new JRadioButton("A.T.", imageIcon);
                    JOptionPane.showMessageDialog(null, radioButton);
                }
            });
        }
    }
    

    Where is the radio button 'selected' icon?

    It seems like a bug to me, though I cannot recall seeing a radio with an icon. How are they supposed to look?


    Time to reach into my 'box of hacks'.

    import javax.swing.*;
    
    class RadioWithImage {
    
        public static void main(String[] args) throws Exception {
            String url = "http://www.gravatar.com/avatar/" +
                "a1ab0af4997654345d7a949877f8037e?s=128";
            final String html = "<html><body><img src='" +
                url +
                "' width=128 height=128>";
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JRadioButton radioButton = new JRadioButton(html);
                    JOptionPane.showMessageDialog(null, radioButton);
                }
            });
        }
    }
    

    Radio Button with Image

    This technique will not work if:

    1. The use-case requires other types of icons (pressed, roll-over, selected etc.)
    2. The button is disabled (it will render incorrectly).
    0 讨论(0)
  • 2021-01-13 10:59

    There is no radio button constructor that allows an image as the content argument instead of a text. The only way to replace the text of a radio button by an image it is generate html and pass it as an argument to the default constructor.

    import javax.swing.*;
    
    class RadioWithImage {
    
        public static void main(String[] args) throws Exception {
            URL url = Windows_ChordsGenerator.class.getResource("/images/img1.png");
    
            final String html = "<html><body><img src='" + url.toString() +"'>";
    
            JRadioButton radioButton = new JRadioButton(html);     
           }
    }
    
    0 讨论(0)
  • 2021-01-13 11:02

    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
    
    0 讨论(0)
提交回复
热议问题