I am setting a background image in JPanel with this
JPanel panel = new JPanel(){
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(ImageHelper.createResizedCopy(image, height, width, false), 0, 0, null);
}
};
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setPreferredSize(new Dimension(height, width));
panel.setOpaque(false);
panel.add(new JLabel("SCIENCE FAIR"));
panel.setBorder(new RoundedBorder(1,2,new Color(100, 200, 112)));
return panel;
I was expecting paintComponents method to respond but somehow paintComponents is not responding but paint is doing the job for painting a background image in JPanel.
This JPanel is the child of another JPanel .
The Parent JPanel is using MigLayout and inside the parent JPanel, I am adding 4 different JPanels with different different background image.
Background image is done but on top of Background image , I am not able to add JLabel, so I doubt the paint method is the issue
You’re painting the entire component after the children are painted — you’re painting right over them, in other words. Never override
paint()in a Swing component; overridepaintComponent()instead, and be sure to callsuper.paintComponent()after painting the background image.