1

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();
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Kinga Garczyńska
  • 309
  • 1
  • 3
  • 6
  • 1. Try setting the frame's content pane to white. 2. Try making the the panels transparent. The panels don't need to be static and all the finals could be fixed by making the virus variables class variables instead – MadProgrammer Dec 29 '12 at 21:10
  • @MadProgrammer, I have changed statics and finals, as you wrote. Thank you. – Kinga Garczyńska Dec 29 '12 at 21:38

2 Answers2

3

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.

Community
  • 1
  • 1
aly
  • 523
  • 2
  • 7
  • Thank you @aly. I have corrected code with frame1 as you adviced and that is ok now. But image file is placed in correct directory, because I can see it in my frame, but there is only image visible without JRadioButton's hole. The image is small, 50 px height and 50 px width. – Kinga Garczyńska Dec 29 '12 at 21:23
  • When you add an `ImageIcon` to a `JRadioButton`, you are replacing that "hole" with something else. If you want that default "hole" and an image as the label for it, check out this answer on a similair question [How to use Image in JRadioButtons instead of text](http://stackoverflow.com/a/6811752/1933512). – aly Dec 29 '12 at 21:31
  • I have used your code, it works, but instead src folder, I had to place an image at bin folder. – Kinga Garczyńska Dec 29 '12 at 22:19
3

There is only image visible without JRadioButton's hole.

The appearance is controlled by the Look & Feel dependent UI delegate, a subclass of ButtonUI. Short of writing your own replacement, you can use ColorIcon, seen here, to render the button as you like—with or without the hole. You can then update the icon using setIcon(), shown here.

Icon czerwonaikona = new ColorIcon(SIZE, Color.red);
JRadioButton czerwony = new JRadioButton("czerwony", czerwonaikona);
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • See also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Dec 29 '12 at 21:42
  • Glad to help; see also this related [example](http://stackoverflow.com/a/6036048/230513) using `JToggleButton`, the parent of `JRadioButton `. – trashgod Dec 30 '12 at 01:01