I am adding a JPanel to a JLayeredPane, but JPanel's paintComponent(); is never invoked:
public class Kj02669 extends JLayeredPane { // change to extend JPanel, then its ok.
Kj02669() {
setPreferredSize(new Dimension(350, 400));
add(new Piece());
// add(new Piece(), 1); // <-- also fails
}
class Piece extends JPanel {
public Piece() {
setPreferredSize(new Dimension(200, 200));
}
@Override
public void paintComponent(Graphics g) { // <-- never invoked
super.paintComponent(g);
System.out.println("why doesn't this print?");
}
}
}
If I have Kj02669 extend JPanel, (not JLayeredPane), then it works. Each JPanel added has their paintComponent(); method automatically invoked. What methods am I forgetting to invoke on JLayeredPane to have an added JPanel's paintComponent() method automatically invoked?
I see a way to get the behaviour I want by overriding paintComponent() in JLayeredPane and from there directly invoke the paint() methods of each added JPanel. But I should not need to do this?
thanks.