6

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jason Rogers
  • 19,194
  • 27
  • 79
  • 112

4 Answers4

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
Eva
  • 4,397
  • 5
  • 43
  • 65
5

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • @Eng. Fouad: I also see him in most of Swing questions seems like mKorble is a swing enthusiast. – Harry Joy Jul 21 '11 at 11:19
  • @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, – mKorbel Jul 21 '11 at 11:21
  • 2
    *"that for me harder as any of PL as I know.."* Programming languages are a lot more *logical* than English. ;) – Andrew Thompson Jul 21 '11 at 13:54
  • 4
    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 text – Jason Rogers Jul 24 '11 at 18:36
  • @Jason Rogers I'm not see any problem in that, what's your issue – mKorbel Jul 24 '11 at 19:34
  • Since I'm not being clear with words I did a paint work of whats my problem and added it in the edit – Jason Rogers Jul 24 '11 at 23:15
  • 4
    this answer is wrong, shouldn't be the accepted one. A correct answer is given by @andrew-thompson. – Trasplazio Garzuglio Jun 28 '12 at 10:21
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).
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • looks like as Java6 forgot for that, but I think that you are here one of Htlm Gurus ... +1 – mKorbel Jul 25 '11 at 08:28
  • @mKorbel 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. :) – Andrew Thompson Jul 25 '11 at 08:45
  • I'm really confused how quickly can coding todays newbie – mKorbel Jul 25 '11 at 08:51
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);     
       }
}
Saralou
  • 63
  • 4
  • 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. – Andrei Vajna II Oct 28 '14 at 16:10