Results 1 to 5 of 5
  1. #1
    JoKeR313 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default Help with adding a Container within a Container

    How do i add a Container to a Container?

    pane.add(Container pane1);

    I've been searching the web for well over an hour now, and i still haven't found anything that relates to my problem. I already understand i can't have multiple layouts in a Container, but i also understand that pane.add accepts Components and Container is part of the Component class.

    I'm currently working on an applet that is going to add various components depending on the setting (whatever the user chooses from the ComboBox). Right now i'm just testing how to get multiple containers in a container, but for every container that doesn't have = getContentPane() wont show up on the screen. Also, if i use getContentPane() on multiple panes, i get an error "java.lang.IllegalArgumentException: adding container's parent to itself". ...and i dont know if it makes a big difference but i'm using .setLayout(null) on all the containers.

    What am i missing to get these other Containers into the main Container?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JoKeR313 View Post
    How do i add a Container to a Container?
    [ ... ]
    What am i missing to get these other Containers into the main Container?
    Every Container is a Component so you should be able to add a Container to another Container. A JComponent (Swing) is also a Container so that shouldn't give any trouble. Of course a Container forbids to add itself to itself. I don't think I understand your problem. A Container can have a single LayoutManager but you can add multiple Containers to a Container so effectively you can divide different areas of a Container into 'parts' with different LayoutManagers. The top level visual components (such a JFrames, JDialogs, JInternalFrames ...) have content panes which are Containers themselves so you can add other Components/Containers to them.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    JoKeR313 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    This setup isn't working for me. I dont know how else to explain it right now.

    Container mainPane = getContentPane();
    Container otherPane = new Container();

    //...CODE

    mainPane.add(Component1);
    mainPane.add(Component2);
    mainPane.add(Component3)

    otherPane.add(Component4);
    otherPane.add(Component5)

    mainPane.add(otherPane);

    The components in mainPane will show, but the components added to otherPane won't show up. I've read about other panes, but right now i'm trying to learn to keep all this in one pane.

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

    Default

    Quote Originally Posted by JoKeR313 View Post
    This setup isn't working for me. I dont know how else to explain it right now.
    Just for the record: are you using Swing components (JPanel, JApplet, JButton etc.) or are you using AWT components( Canvas, Applet, Button etc.) You shouldn't mix them both.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    JoKeR313 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    Right now, i'm using these imports...


    import java.awt.Container;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;

    import javax.swing.ButtonGroup;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JLabel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;

    The thing that doesn't sound right is that i read that i could use Lightweight and Heavyweight together. Although, i'm not trying to mix them both and its recommended not to. Plus the book i have been using to learn all this generally uses swing, but also uses some of awt like Events and the Container.

Similar Threads

  1. How to center a window with Container
    By fernandoj in forum AWT / Swing
    Replies: 5
    Last Post: 05-10-2010, 12:00 AM
  2. Applet as container .
    By pawankumarom in forum Java Applets
    Replies: 5
    Last Post: 04-23-2009, 07:17 AM
  3. Replies: 3
    Last Post: 04-22-2009, 12:01 AM
  4. AWT can we make a Tabbed container?
    By Panchitopro in forum AWT / Swing
    Replies: 0
    Last Post: 05-15-2008, 11:31 PM

Tags for this Thread

Posting Permissions

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