Search code examples
javaswingjradiobuttonsetbackground

can't set JPanel color and JRadioButton invisibility


I have problem with setting JPanel and JFrame color to white, though I used panel.setBackground(Color.white). The second problem is that setting ImageIcon in JRadioButton constructor causes that JRadioButton is invisible. Here is my code:

public class proby {

    static JPanel panel = new JPanel();
    static JPanel panel2 = new JPanel();

    private void createAndShowGUI() {
        final ImageIcon zielonaikona = new ImageIcon("green2.png");
        final ImageIcon czerwonaikona = new ImageIcon("red2.png");
        final ImageIcon niebieskaikona = new ImageIcon("blue.png");
        final ImageIcon szaraikona = new ImageIcon("grey.png");
        JFrame frame1 = new JFrame("MasterMind");
        final JRadioButton zielony = new JRadioButton(zielonaikona);
        zielony.setBackground(Color.WHITE);
        final JRadioButton czerwony = new JRadioButton("czerwony");
        czerwony.setBackground(Color.white);
        final JRadioButton niebieski = new JRadioButton("niebieski");
        niebieski.setBackground(Color.white);
        final JRadioButton szary = new JRadioButton("szary");
        szary.setBackground(Color.white);
        zielony.setSelected(true);
        ButtonGroup gruparadio = new ButtonGroup();
        gruparadio.add(zielony);
        gruparadio.add(czerwony);
        gruparadio.add(niebieski);
        gruparadio.add(szary);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton akceptuj = new JButton("Akceptuj");

        akceptuj.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JLabel label2;
                if (zielony.isSelected()) {
                    label2 = new JLabel(zielonaikona);
                } else if (czerwony.isSelected()) {
                    label2 = new JLabel(czerwonaikona);
                } else if (szary.isSelected()) {
                    label2 = new JLabel(szaraikona);
                } else {
                    label2 = new JLabel(niebieskaikona);
                }
                panel2.add(label2);
                panel2.revalidate();
            }
        });

        BoxLayout layout = new BoxLayout(panel, BoxLayout.Y_AXIS);
        BoxLayout layout2 = new BoxLayout(panel2, BoxLayout.Y_AXIS);
        panel.setLayout(layout);
        panel2.setLayout(layout2);
        panel.add(zielony);
        panel.add(czerwony);
        panel.add(niebieski);
        panel.add(szary);
        panel.add(akceptuj);
        panel.setBackground(Color.WHITE);
        panel2.setBackground(Color.white);
        frame1.getContentPane().add(panel);
        frame1.getContentPane().add(panel2);
        BoxLayout layout3 = new BoxLayout(frame1.getContentPane(), BoxLayout.Y_AXIS);
        frame1.setLayout(layout3);
        frame1.setBackground(Color.white);
        frame1.setSize(300, 300);
        frame1.setVisible(true);
    }

    public static void main(String[] args) {
        proby kk = new proby();
        kk.createAndShowGUI();
    }
}

Solution

  • If you want to set your JFrame background color to white, you have to get the ContentPane and set that to white:

    frame1.getContentPane().setBackground(Color.white);
    

    Take a look at JFrame.setBackground() not working — why?

    As for the ImageIcon problem, it's probably because you don't have the image file at the path you indicated. (Which in your case is just inside the project folder).

    Edit: Now that I know what you're trying to do with the ImageIcon, I came up with this after seeing Andrew Thompson's trick

    String imageText = "<html><img src=\""+this.getClass().getResource("green2.png")
                .toString()+"\"></img></html>";
    JRadioButton zielony = new JRadioButton(imageText);
    

    However, it does involve you placing the images inside the src folder, not the project one.