Hi guys, please see the following code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
final JFrame f=new JFrame("foo");
final JPanel c=new JPanel(null);
f.setContentPane(c);
c.setPreferredSize(new Dimension(500,500));
final JPanel a=new JPanel(null){
@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.red);
g.fillRect(0, 0, getWidth(), getHeight());
}
};
a.setBounds(0,0,300,300);
c.add(a);
final JPanel b=new JPanel(null){
@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.green);
g.fillRect(0, 0, getWidth(), getHeight());
}
};
b.setBounds(200,200,500,500);
c.add(b);
c.setComponentZOrder(a, 0);
f.pack();
f.setVisible(true);
f.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
b.repaint();
}
});
}
}
It basically just draws two panels on top of third one: panel A, which is red, and panel B, which is green. The red panel A has lower z-order and is therefore painted over the panel B. Now if we force the panel B to repaint itself (just click on the JFrame, outside of both panels A and B), suddenly the panel B paints OVER the panel A.
If I switch to using JComponent instead of JPanel, it works correctly and B will not paint over A. It would seem that JPanel simply ignores the Z-order. So, the solution seems to be to use JComponent instead of JPanel. Just out of curiosity - is this Z-order-ignoring behavior normal for JPanel?