Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

adding JPanel to JLayeredPane

843807Sep 1 2010 — edited Sep 2 2010
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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 30 2010
Added on Sep 1 2010
2 comments
782 views