1

I would like to have image on my JPanels an also have components such as JSlider and JRadioButton on my JPanel. I derived a class from JPanel and overrided method paintComponent as you see. This is a good way to have image on JPanel.

public void paintComponent(Graphics g)
{
    /*create image icon to get image*/
    ImageIcon imageicon = new ImageIcon(imageFile); //getClass().getResource(imageFile)
    Image image = imageicon.getImage();

    /*Draw image on the panel*/
    super.paintComponent(g);

    if (image != null)
        g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}

However I have some problems. When I add components such as JSlider, JRadioButton or another JPanel on my ImagePanel; this component's background remains as default and not background picture. I don't know how to set this image as background of this components. please guide me.

regards

julianfperez
  • 1,726
  • 5
  • 38
  • 69
sajad
  • 2,094
  • 11
  • 32
  • 52

3 Answers3

1

you must set opaque properties of other component to false.

jRadioButton.setOpaque(false);

for example :

ImagePanel with RadioButton on it.

mehdi shahdoost
  • 1,469
  • 5
  • 17
  • 27
1
jRadioButton.setOpaque(false);

Will work for many look-and-feels, but if you want it to work with Nimbus you should also set the background color to be transparent:

jRadioButton.setBackground(new Color(0,0,0,0));

See this question for more details.

Community
  • 1
  • 1
finnw
  • 47,861
  • 24
  • 143
  • 221
0

Doesn't setOpaque(false) for all the other components help?

maaartinus
  • 44,714
  • 32
  • 161
  • 320