Why I obtain this error when I try to add the Container object to the main JFrame object using Swing?

StackOverflow https://stackoverflow.com/questions/19858221

  •  29-07-2022
  •  | 
  •  

Question

I am absolutly new in Java Swing application and I have the following problem.

I have this simple LoginFrame2 class:

package com.test.login;

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import net.miginfocom.swt.MigLayout;

import org.jdesktop.application.SingleFrameApplication;

public class LoginFrame2 extends SingleFrameApplication {

    public static void main(String[] args) {
        System.out.println("DENTRO: LoginFrame() ---> main()");
        launch(LoginFrame2.class, args);
    }

    @Override
    protected void startup() {
        // TODO Auto-generated method stub
        System.out.println("Inside startup()");

        JFrame mainFrame = this.getMainFrame();         // main JFrame that represents the Windows
        mainFrame.setTitle("XCloud Login");

        Container mainContainer = mainFrame.getContentPane();       // main Container into the main JFrame

        // JPanel creation and settings of the MigLayout on it:
        JPanel externalPanel = new JPanel();
        externalPanel.setLayout(new net.miginfocom.swing.MigLayout("fill"));

        externalPanel.add(new JLabel("Username"));

        mainContainer.add(externalPanel);
        mainFrame.add(mainContainer);


    }

}

As you can see I am using the SingleFrameApplication abstrac class of the JDesktop framework that simply provide to me one primary JFrame (this abstract class also takes care of component property injection, exit processing, and saving/restoring session state in a way that's appropriate for simple single-frame applications).

When the class is executed the startUp() method is executed.

As you can see I have performed the following operations:

  1. I take the main Jframe using the getMainFrame() method and I put its reference into mainFrame variable

  2. I take the main Container (that is into the mainFrame object) calling the getContentPane() method

  3. Then I create a JPanel object named externalPanel in which I put an initialized JLabel that will show a string.

  4. Finally I add my JPanel object to the Container object and then I try to add the Container to the main JFrame object.

The problem is that when I execute this class I obtain the following error message:

DENTRO: LoginFrame() ---> main()
Inside startup()
8-nov-2013 12.35.23 org.jdesktop.application.Application$1 run
GRAVE: Application class com.test.login.LoginFrame2 failed to launch
java.lang.IllegalArgumentException: adding container's parent to itself
    at java.awt.Container.checkAddToSelf(Container.java:418)
    at java.awt.Container.addImpl(Container.java:1036)
    at java.awt.Container.add(Container.java:957)
    at javax.swing.JFrame.addImpl(JFrame.java:540)
    at java.awt.Container.add(Container.java:363)
    at com.test.login.LoginFrame2.startup(LoginFrame2.java:38)
    at org.jdesktop.application.Application$1.run(Application.java:187)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
    at java.awt.EventQueue.access$400(EventQueue.java:81)
    at java.awt.EventQueue$2.run(EventQueue.java:633)
    at java.awt.EventQueue$2.run(EventQueue.java:631)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" java.lang.Error: Application class com.test.login.LoginFrame2 failed to launch
    at org.jdesktop.application.Application$1.run(Application.java:192)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
    at java.awt.EventQueue.access$400(EventQueue.java:81)
    at java.awt.EventQueue$2.run(EventQueue.java:633)
    at java.awt.EventQueue$2.run(EventQueue.java:631)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: java.lang.IllegalArgumentException: adding container's parent to itself
    at java.awt.Container.checkAddToSelf(Container.java:418)
    at java.awt.Container.addImpl(Container.java:1036)
    at java.awt.Container.add(Container.java:957)
    at javax.swing.JFrame.addImpl(JFrame.java:540)
    at java.awt.Container.add(Container.java:363)
    at com.test.login.LoginFrame2.startup(LoginFrame2.java:38)
    at org.jdesktop.application.Application$1.run(Application.java:187)
    ... 14 more

I think that the problem is the adding of the Container object in its Jframe object. But why? From what I have understand the Container object is the object wrapped inside the JFrame...where is the problem? how can I solve it?

Tnx

Andrea

Was it helpful?

Solution

you are triying add container in this container. Problem is here

Container mainContainer = mainFrame.getContentPane();
...
mainFrame.add(mainContainer);

this JFrame mainFrame = this.getMainFrame(); already have ContentPane, and you adding one more. Each JFrame have 3 panels -> glassPane, contentPane and layerdPane.

Try add elements in mainContainer (that already included in mainFrame).

OTHER TIPS

mainContainer is already derived from the JFrame.

JFrame mainFrame = this.getMainFrame();         
    // main JFrame that represents the Windows
mainFrame.setTitle("XCloud Login");

Container mainContainer = mainFrame.getContentPane();
 ..
mainFrame.add(mainContainer);
mainContainer = mainFrame.getContentPane();
//...
mainFrame.add(mainContainer);//wrong

There's no reaqson to add mainContainer back to mainFrame, and adding children to a JFrame is completely wrong in general.

You should simply add children to mainContainer, as you already do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top