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

    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,316
    Blog Entries
    1
    Rep Power
    26

    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
    4

    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 online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,432
    Blog Entries
    7
    Rep Power
    20

    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. what is the difference
    By ron87 in forum New To Java
    Replies: 5
    Last Post: 01-04-2011, 04:31 PM
  2. Replies: 1
    Last Post: 02-27-2010, 12:35 AM
  3. What is the difference between
    By arnab321 in forum New To Java
    Replies: 2
    Last Post: 01-19-2009, 04: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
  •