adding container's parent to itself

Posted on 2006-07-23
Medium Priority
Last Modified: 2012-08-13

hello there,

i am trying to insert many jbuttons into a jpanel.but i get this error.can anyone help me solve this

java.lang.IllegalArgumentException: adding container's parent to itself

private JPanel createTerminalPanel()
          JPanel pKey = new JPanel(new GridLayout());
          innerCardPanel = new JPanel(new CardLayout());
          pKey.add(new JButton("C"));
        pKey.add(new JButton("MR"));
        pKey.add(new JButton("M-"));
        pKey.add(new JButton("M+"));
        pKey.add(new JButton("7"));
        pKey.add(new JButton("8"));
        pKey.add(new JButton("9"));
        pKey.add(new JButton("/"));
        pKey.add(new JButton("4"));
        pKey.add(new JButton("5"));
        pKey.add(new JButton("6"));
        pKey.add(new JButton("*"));
        pKey.add(new JButton("1"));
        pKey.add(new JButton("2"));
        pKey.add(new JButton("3"));
        pKey.add(new JButton("-"));
        pKey.add(new JButton("0"));
        pKey.add(new JButton("."));
        pKey.add(new JButton("="));
        pKey.add(new JButton("+"));
        innerCardPanel.add(pKey, MDF_PANEL);
        pKey.add(innerCardPanel, BorderLayout.CENTER);
          return pKey;
Question by:zolf
  • 5
  • 3
LVL 23

Assisted Solution

Ajay-Singh earned 500 total points
ID: 17162830
> pKey.add(innerCardPanel, BorderLayout.CENTER);

i think you don't need this
LVL 86

Expert Comment

ID: 17162832
>>innerCardPanel.add(pKey, MDF_PANEL);

should be

innerCardPanel.add(pKey, "Calculator");
LVL 86

Expert Comment

ID: 17162833
Also, you don't want to add the card panel to the grid layout
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

LVL 86

Accepted Solution

CEHJ earned 500 total points
ID: 17162834
(which you're doing here)

>>pKey.add(innerCardPanel, BorderLayout.CENTER);

Author Comment

ID: 17162836

can you give me an example where i can add many buttons into a jpanel.
LVL 86

Expert Comment

ID: 17162838
That bit's OK. You need to determine what you want to do with the CardLayout. What's its purpose? At the moment the reference to it is lost

Author Comment

ID: 17162867

i use the cardlayout to switch between two views.my application which i am working on is very complicated and vast.
i managed to get some buttons on a panel and show.but i have got other error which i have to look into.
thanks anyway.

Author Comment

ID: 17162871

that is the reason i cannot paste all the code in here.
LVL 86

Expert Comment

ID: 17162878

OK. If you want to add that calculator panel to a CardLayout, you can pass a reference to the CardLayout into the method, or just add the return value directly to the CardLayout

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question