2

I am using Swing and JRadioButton in my application. I need to set image and text on my button. For that I am using this:

JRadioButton button1 = new JRadioButton("text", iconpath, false);

But what it giving output is it's hiding radio button and just showing the image.

How to over come this problem, any suggestion? and can we Create something for similar problem for JCheckbox too?

Kishan Bheemajiyani
  • 3,429
  • 5
  • 34
  • 68
  • 2
    The icon overrides the "radio" button thingy in place of the usual circle. I tend to use a `JLabel` to hold the icon and text, depending on what you want to achieve – MadProgrammer Apr 03 '14 at 07:19
  • I want that circle text and image too. shown into that. – Kishan Bheemajiyani Apr 03 '14 at 07:20
  • 1
    Yep, create a `JRadioButton`, no text or icon. Create a `JLabel` with the icon and text. Set the label's `labelFor` property, add the two together onto your screen. You might find it easier adding them a `JPanel` first. – MadProgrammer Apr 03 '14 at 07:21
  • @MadProgrammer But what i am doing is i need to add it dynamically m not dragging that components. what if i need that text value onclick and i need to click that Radiobutton on text click. – Kishan Bheemajiyani Apr 03 '14 at 07:26
  • Then keep and association of the radio button and label, may be even create a custom component – MadProgrammer Apr 03 '14 at 07:28
  • @MadProgrammer Custom Component? how to do that? and ya can i have example bro. – Kishan Bheemajiyani Apr 03 '14 at 07:29

1 Answers1

5

Setting the icon of a JRadioButton or JCheckBox replaces the default glyph used by these controls - I know, annoying.

The simplest solution is to simply create a JLabel which can be associated with the JRadioButton, perhaps using some kind of Map to maintain the linkage between

A more, long term solution, might be to create a custom component which marries the concept into a custom and re-usable component, for example...

public class XRadioButton extends JPanel {

    private JRadioButton radioButton;
    private JLabel label;

    public XRadioButton() {
        setLayout(new GridBagLayout());
        add(getRadioButton());
        add(getLabel());
    }

    public XRadioButton(Icon icon, String text) {
        this();
        setIcon(icon);
        setText(text);
    }

    protected JRadioButton getRadioButton() {
        if (radioButton == null) {
            radioButton = new JRadioButton();
        }
        return radioButton;
    }

    protected JLabel getLabel() {
        if (label == null) {
            label = new JLabel();
            label.setLabelFor(getRadioButton());
        }
        return label;
    }

    public void addActionListener(ActionListener listener) {
        getRadioButton().addActionListener(listener);
    }

    public void removeActionListener(ActionListener listener) {
        getRadioButton().removeActionListener(listener);
    }

    public void setText(String text) {
        getLabel().setText(text);
    }

    public String getText() {
        return getLabel().getText();
    }

    public void setIcon(Icon icon) {
        getLabel().setIcon(icon);
    }

    public Icon getIcon() {
        return getLabel().getIcon();
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366