why
why

Reputation: 1295

Does getContentPane().add() mean the same as add()

Does getContentPane().add() mean the same as add() ?

public class TestFrame extends JFrame{
    public TestFrame() {
        JLabel label = new JLabel("jo");
        getContentPane().add(label);
        add(label);
    }
}

Upvotes: 10

Views: 10833

Answers (3)

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

Mostly. To make things "easier", addImpl was changed to forward to the content pane, but in uncommon corner cases it doesn't (for instance, the content pane needs to be added somehow).

This method is overridden to conditionally forward calls to the contentPane.

Upvotes: 3

John S.
John S.

Reputation: 2007

Yes, because if you notice they both are from

Class Container
java.lang.Object
  java.awt.Component
      java.awt.Container
add()

JLabel extends container and getContentPane() returns container.

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168835

Does getContentPane().add() mean the same as add() ?

Yes, since 1.5+.

Upvotes: 15

Related Questions