sajad
sajad

Reputation: 2174

JPanel image and components

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

Upvotes: 1

Views: 994

Answers (3)

finnw
finnw

Reputation: 48639

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.

Upvotes: 1

mehdi shahdoost
mehdi shahdoost

Reputation: 1489

you must set opaque properties of other component to false.

jRadioButton.setOpaque(false);

for example :

ImagePanel with RadioButton on it.

Upvotes: 1

maaartinus
maaartinus

Reputation: 46482

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

Upvotes: 0

Related Questions