Results 1 to 4 of 4
  1. #1
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    5

    Default Difference between add() and getContentPane().add()?

    What is the difference between getContentPane().add() and just add();

    Java Code:
    JFrame frame = new JFrame();
    JButton button = new JButton("Corn");
    
    [COLOR="Red"]frame.add(button);
    [/COLOR]
    // or...............
    [COLOR="Red"]
    frame.getContentPane().add(button);[/COLOR]

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    27

    Default

    What is the difference between getContentPane().add() and just add();
    Nothing. add() was added to JFrame as a convenience method. As the JFrame API states (always the first place to look):

    As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:

    frame.add(child);

    And the child will be added to the contentPane. The content pane will always be non-null.

  3. #3
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    5

    Default

    Thanks gain :)

    The API is the first place i looked and I know I read that very quote in the JFrame API. My issue is...

    Nothing. add() was added to JFrame as a convenience method

    makes more sense then

    As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:

    frame.add(child);

    And the child will be added to the contentPane. The content pane will always be non-null.

    I guess i missed it :/ but now I see.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,051
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by AcousticBruce View Post
    Thanks gain :)

    The API is the first place i looked and I know I read that very quote in the JFrame API. My issue is... [ ... ]
    Well, it can be a bit tricky: the JFrame class doesn't implement an add( ... ) method at all. It leaves that to a superclass, the Container class. That method implements an add( ... ) method and calls a protected addImpl( ... ) method. That's the method that is overridden by the JFrame class and it 'delegates' (an OO-word) the same to its content pane. It doesn't clearly show from the API documentation on a first read.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. what is the difference
    By ron87 in forum New To Java
    Replies: 5
    Last Post: 01-04-2011, 05:31 PM
  2. Replies: 1
    Last Post: 02-27-2010, 01:35 AM
  3. What is the difference between
    By arnab321 in forum New To Java
    Replies: 2
    Last Post: 01-19-2009, 05:49 AM
  4. difference
    By nishant in forum New To Java
    Replies: 2
    Last Post: 09-17-2008, 06:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •