Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java swing radioButton with changing, clickable icon

Designing a questionary, the scope of answer can be elected by radioButtons. To display a greater clickable area (the application is for touchscreen), I layed icon_1 over the radiobuttons.

Every mouseclick can change the displayed icon to icon_2 and permantly vice versa. I am sorry, using

jRadioButtonActionPerformed 
ImageIcon o_ButtonIcon = new ImageIcon ("....") 
jRadioButton.setIcon(Icon m_ButtonIcon).

I get no changing, clickable image. Can you please give me a helping hand?

like image 648
user1860368 Avatar asked Nov 29 '25 20:11

user1860368


1 Answers

Seems to be working fine.

Post an SSCCE to show specific problems.

enter image description here

enter image description here

Here is example ( i do not recommend getScaledInstance(..) just used it for quick example)

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;

public class Test {

    private ImageIcon ii1;
    private ImageIcon ii2;
    private JRadioButton jrb = new JRadioButton("Click me :)");
    private JFrame frame = new JFrame();

    public Test() {
        try {
            ii1 = new ImageIcon(ImageIO.read(new URL("http://cdn.macrumors.com/article/2010/09/03/145454-itunes_10_icon.jpg")).getScaledInstance(48, 48, Image.SCALE_SMOOTH));
            ii2 = new ImageIcon(ImageIO.read(new URL("http://www.quarktet.com/Icon-small.jpg")).getScaledInstance(48, 48, Image.SCALE_SMOOTH));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        initComponents();
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }

    private void initComponents() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jrb.setIcon(ii1);
        jrb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (jrb.getIcon() == ii1) {
                    jrb.setIcon(ii2);
                } else {
                    jrb.setIcon(ii1);
                }
            }
        });

        frame.add(jrb);
        frame.pack();
        frame.setVisible(true);
    }
}
like image 117
David Kroukamp Avatar answered Dec 02 '25 10:12

David Kroukamp