开发者

JRadioButton: how to replace text by IconImage?

开发者 https://www.devze.com 2023-03-22 11:50 出处:网络
I want to replace the开发者_高级运维 text in my radio button list by an icon. I\'ve tried this:

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:

JRadioButton: how to replace text by IconImage?

But I want it to end up with this:

JRadioButton: how to replace text by IconImage?


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


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

JRadioButton: how to replace text by IconImage?

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

JRadioButton: how to replace text by IconImage?

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).


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

精彩评论

暂无评论...
验证码 换一张
取 消