I have a JFrame window to which I am trying to add 3 panels. One panel is for input (text areas and their labels which I already have arranged within the panel), one is for a set of buttons, and one is for output. I want the the input and button panels to share the left half of the window (input panel on top and button panel below) and the output panel to take up the entire right half of the window by itself. Because of this, GridLayout , BoxLayout , and BorderLayout don't work by themselves. I just tried making one container and adding the input and button panels to it with the input panel above and the button panel below. I then made a second container and tried adding the first container to it on the left side and the output panel to the right side. The compiler told me I can't add a container's parent to itself. How can I make two panels share the left side and have a third have the right side to itself?

Recommended Answers

All 10 Replies

maybe typos mistake,check that again because one cotainer you can add just one, that standard how to lay GUI, nice way

hmmm these "panels" are declared (at all) as JPanels ???,

I'm sorry I don't understand the first part of your reply. Yes the panels are declared as JPanels . The panels work with other layouts it's just that when I try to add one container to another to get the layout I want, I get an error.

Also, the code compiles but the error occurs when I run it. I'll post that section of the code and the error I'm getting.

My code:

public CoffeeGUI()
   {
      myGUI = createJFrame();

      Container leftSide = myGUI.getContentPane();
      leftSide.setLayout( new BorderLayout() );

      inputPanel = createInputPanel();
      leftSide.add( "North", inputPanel );

      buttonPanel = createButtonPanel();
      leftSide.add( "South", buttonPanel );

      Container bothSides = myGUI.getContentPane();
      bothSides.setLayout( new BorderLayout() );

      bothSides.add( "West", leftSide );

      displayPanel = createDisplayPanel();
      bothSides.add( "East", displayPanel );
   }

Error message:

Exception in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself
	at java.awt.Container.checkAddToSelf(Container.java:420)
	at java.awt.Container.addImpl(Container.java:1038)
	at java.awt.Container.add(Container.java:379)
	at CoffeeGUI.<init>(CoffeeGUI.java:65)
	at CoffeeGUI.main(CoffeeGUI.java:151)

remove anything about container or ContentPane, that very old .... (set backGround Color for JFrame),

usage of container is worng, forgot about that, LayoutManaget solved that ..., don't forget set the PreferredSize for positions excepts Center (that works automatically),

hmmm

myFrame.add(somePanel, BorderLayout.WEST); NORTH, SOUTH..... CENTER
myFrame.pack();
myFrame.setVisible(true);
} //end of buid GUI

Hmmm... using Container and ContentPane is the way my professor taught us to do it. This is for a project where are expected to research and use some things we have not been taught though. Is there a way to do it without re-doing so many things and just keeping most of what I have other than the layout? If not I'll try changing it but I'd like to keep most of it the way it is as I was taught that way and understand it.

leftSide is the contentpane for the window (previous listing line 5)
bothSides is also the contentpane for the window (previous listing line 14)
so on line 17 you are trying to add the content pane to itself, == error.

At a guess, leftSide should be a new container.

I made leftSide a container and that worked. I didn't realize that I hadn't done that in the first place. Thank you very much.

:-) yes your profesor had right, JFrame is Top Layout Container, and every todays Layout Managers automatically build place for JComponents based on ContentPane :-)

I made leftSide a container and that worked. I didn't realize that I hadn't done that in the first place. Thank you very much.

OK, glad to help. Mark this solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.