Results 1 to 4 of 4
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default simple container question

    Why you can not add a frame to a container (you can add eg a JPanel).
    (I found the explanation on the net not very satisfying)


    Java Code:
        panel = new JPanel();
      //  frame = new JFrame();  NOT!
         
          Container contentPane = getContentPane();
          contentPane.add(panel);
      //  contentPane.add(frame);  NOT!

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: simple container question

    JFrame is a top-level container i.e. it is the root in the chain of components. Still the question rises, why do you want to add a frame to a panel? That doesn't make sense. What are you trying to achieve? If you are looking for an MDI style application, JInternalFrame is your friend.

    Using Top-Level Containers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    How to Use Internal Frames (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default Re: simple container question

    The reason has to do with how Swing works. Frames are tied to OS presentation manager objects. Everything else is handled directly by Swing, using the frame. So, you can't have a frame in a frame. However, there are Swing containers, including JPanel. These can be layered.
    The Java Tutorial. Read it.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,418
    Rep Power
    5

    Default Re: simple container question

    BTW, JFrame overrides at least one container method to facilitate adding components directly to the content pane. So all I ever use is add(). It is mainly a convenience method.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Please help. Simple question
    By owencain in forum New To Java
    Replies: 14
    Last Post: 06-16-2011, 01:07 AM
  2. Help with adding a Container within a Container
    By JoKeR313 in forum New To Java
    Replies: 4
    Last Post: 01-02-2011, 04:59 AM
  3. Java Generic Container Question - please help!!!
    By zhoujackji in forum New To Java
    Replies: 3
    Last Post: 11-13-2010, 11:12 AM
  4. Replies: 3
    Last Post: 04-21-2009, 11:01 PM
  5. Simple Question
    By barusk in forum Networking
    Replies: 13
    Last Post: 03-04-2009, 07:33 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
  •