Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1834
  • Last Modified:

adding container's parent to itself


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;
    }
0
zolf
Asked:
zolf
  • 5
  • 3
2 Solutions
 
Ajay-SinghCommented:
> pKey.add(innerCardPanel, BorderLayout.CENTER);

i think you don't need this
0
 
CEHJCommented:
>>innerCardPanel.add(pKey, MDF_PANEL);

should be

innerCardPanel.add(pKey, "Calculator");
0
 
CEHJCommented:
Also, you don't want to add the card panel to the grid layout
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
(which you're doing here)

>>pKey.add(innerCardPanel, BorderLayout.CENTER);
0
 
zolfAuthor Commented:


can you give me an example where i can add many buttons into a jpanel.
0
 
CEHJCommented:
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
0
 
zolfAuthor Commented:

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.
0
 
zolfAuthor Commented:

that is the reason i cannot paste all the code in here.
0
 
CEHJCommented:
:-)

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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now