JRadioButton: how to replace text by IconImage?
Asked Answered
P

4

7

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:

Enter image description here

But I want it to end up with this:

Enter image description here

Pyrrolidine answered 21/7, 2011 at 11:2 Comment(0)
K
5

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

Koppel answered 21/7, 2011 at 11:6 Comment(7)
@Eng. Fouad: I also see him in most of Swing questions seems like mKorble is a swing enthusiast.Swadeshi
@Eng.Fouad because there are lots of fun, kind people with peaceFullFaces, and learning English language :-) that for me harder as any of PL as I know,Koppel
"that for me harder as any of PL as I know.." Programming languages are a lot more logical than English. ;)Millstream
Sorry but its not what I'm looking for, I want to keep the existing radio and replace the text by an image. as here is replaces the default radio button by the image and keeps the textPyrrolidine
@Jason Rogers I'm not see any problem in that, what's your issueKoppel
Since I'm not being clear with words I did a paint work of whats my problem and added it in the editPyrrolidine
this answer is wrong, shouldn't be the accepted one. A correct answer is given by @andrew-thompson.Penalty
P
9

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
Priscian answered 9/10, 2011 at 22:16 Comment(0)
K
5

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

Koppel answered 21/7, 2011 at 11:6 Comment(7)
@Eng. Fouad: I also see him in most of Swing questions seems like mKorble is a swing enthusiast.Swadeshi
@Eng.Fouad because there are lots of fun, kind people with peaceFullFaces, and learning English language :-) that for me harder as any of PL as I know,Koppel
"that for me harder as any of PL as I know.." Programming languages are a lot more logical than English. ;)Millstream
Sorry but its not what I'm looking for, I want to keep the existing radio and replace the text by an image. as here is replaces the default radio button by the image and keeps the textPyrrolidine
@Jason Rogers I'm not see any problem in that, what's your issueKoppel
Since I'm not being clear with words I did a paint work of whats my problem and added it in the editPyrrolidine
this answer is wrong, shouldn't be the accepted one. A correct answer is given by @andrew-thompson.Penalty
M
5

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).
Millstream answered 25/7, 2011 at 4:21 Comment(3)
looks like as Java6 forgot for that, but I think that you are here one of Htlm Gurus ... +1Koppel
@Koppel Oh no! I may seem an HTML guru to Java programmers, but to the web-designers, I'm a newbie. But thanks for saying it. ;) Yours was a good answer by the way, +1. I like answers that include links to the latest JDocs. :)Millstream
I'm really confused how quickly can coding todays newbieKoppel
T
4

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);     
       }
}
Ting answered 16/10, 2014 at 9:51 Comment(1)
Great answer! This is more useful than @Andrew Thompson's answer because it uses an image actually bundled with the jar, and not an external resource.Gottuard

© 2022 - 2024 — McMap. All rights reserved.