Hi,

If I have a class, defined in UML, that can contain itself (0 or more times) and if that class is a JPanel how would I implement it without the "Illegal Argument" runtime error?

please see the attach file for a uml class diagram.

Can anyone help me?

Thanks.

Hi jpavao and welcome to DaniWeb :)

It won't actually contain itself, it will contain another instance of the same class. For example, if it were a JPanel, your code would look something like this:

JPanel parentPanel = new JPanel();
JPanel childPanel = new JPanel();
parentPanel.add(childPanel); // --> NOT parentPanel.add(parentPanel);
commented: good helpful attitude :) +4

Thank you!
After a few hours of work our minds get weeker...and with deadlines comming things get worst.

Of course your sugesttion worked out. I don't get anymore a runtime error.

Still, the inner Panel doesn't show itself. I know it was added to the container because I called the Container's ComponentAdded method and it returns that a componente was added (and refers the instance name to). So everything I ask for help is working and I thank you very much for that.

BUT how do I get the inner Panel to be seen if it is there? I used the code bellow. Any help?

Thanks again

private void addInColecaoBtActionPerformed(java.awt.event.ActionEvent evt) {                                               
// ColectionBean is the inner component to be added to the
// Container inColecaoPanel
// The code is not very clean because of my sucessive atemptings
// to see what was wrong

        ColectionBean cb=new ColectionBean();
        cb.setVisible(true);
        cb.revalidate();
        this.inColecaoPanel.add(cb);
       
        
        this.inColecaoPanel.setVisible(true);
        this.inColecaoPanel.repaint();
    }                                              

    private void inColecaoPanelComponentAdded(java.awt.event.ContainerEvent evt) {
// This is just a call to the Container's ComponentAdded()
// It returns Component added ColectionBean

        System.out.println("Adicionado componente "+evt.getChild()+" a "+evt.getSource());
    }

Hi jpavao and welcome to DaniWeb :)

It won't actually contain itself, it will contain another instance of the same class. For example, if it were a JPanel, your code would look something like this:

JPanel parentPanel = new JPanel();
JPanel childPanel = new JPanel();
parentPanel.add(childPanel); // --> NOT parentPanel.add(parentPanel);

So you can see inColecaoPanel but you can't see the panel inside of it? Did you try calling inColecaoPanel.revalidate() after you called setVisible on it? Oh, and if you attach your project as a zip file, I'll run it on my machine and try to figure out what is going on.

Hi BestJewSinceJC!
Thank you for your response.

Well I tried your sugestions but it didn't workout either!
I'm working with Netbeans 6.7.1. I'm sending you the zip folder where the project is.

This is a project where I build and test beans to use in other applications, not a working project, so there will be some other code not related.

The code that matters is in the same package (org.utad.ua.ect.essua.beans) and the file where I'm editing is ColectionBean.java

1- In this project I have a "ItemBean" class which is a bean. This bean was added to "ItemStructBean" wich is also a bean.
2- Then I have a ColectionBean class, also a Bean, that contains an ItemStructBean and some other stuff. THIS IS THE BEAN with problems at line 198-215. In there you can see the "inColecaoPanel" where I'm adding a new instance of a "ColectionBean". This is the one that can be seen but nothing can be see inside. However executing lines 217-220, where a inColecaoPanelComponentAdded method was added we can see that a component is actually added and it is the "ColectionBean" as it should be.

3- Finally the ColectionBean is added to colectionBeanStruct bean which is the outmost bean that may be added to the netbeans toolbar and then dragged to the MyForm.java wich is a Frame container so it can be runned.

The goal is press all those buttons (+i and -i will add and remove Items and +c and -c will add and remove collections. That works fine with colectionStructBean. What is wrong is whem "+c" is pressed in the ColectionBean: nothing is added to the panel wich is bellow with the orange border - the inColecaoPanel).

Confused? Sorry! My mess.
Any way everything runs well with other beans except for this ColectionBean. Appreciate if you can do something, but no hard feelings if you get lost in my Java Jungle.
Thank you for your attention.

So you can see inColecaoPanel but you can't see the panel inside of it? Did you try calling inColecaoPanel.revalidate() after you called setVisible on it? Oh, and if you attach your project as a zip file, I'll run it on my machine and try to figure out what is going on.

Hi it's me again just to send you the "executable".

Thanks again.

So you can see inColecaoPanel but you can't see the panel inside of it? Did you try calling inColecaoPanel.revalidate() after you called setVisible on it? Oh, and if you attach your project as a zip file, I'll run it on my machine and try to figure out what is going on.

I think you didn't create the executable properly, I get an error when I run it that says "cannot find main class". Something wrong with your manifest file probably. I'll take a look at your source code though.

Sorry!
I rebuilt it and tested again. Here is it.

I think you didn't create the executable properly, I get an error when I run it that says "cannot find main class". Something wrong with your manifest file probably. I'll take a look at your source code though.

For anyone that is using Windows, here is the source code he posted. Now it will unzip correctly.

commented: Thanks. Vista wouldn't let me unzip it initially. +4

Change the code in your action performed method to the following:

JOptionPane.showMessageDialog(null, "Adding JPanel");
        ColectionBean cb=new ColectionBean();
        cb.setSize(500,500);
        cb.add(new JLabel("THIS IS THE NEW PANEL YO"));
        cb.setVisible(true);
        this.inColecaoPanel.add(cb);
        cb.revalidate();
        System.out.println("É Visivel? "+cb.isVisible());
        this.inColecaoPanel.setVisible(true);
        this.inColecaoPanel.revalidate();

And when you click the button you will see that your panel shows up, not like you'd want it to, (i.e. parts of it are cut off and it doesn't look pretty) but it does show up. You can fix this by playing with the setPreferredSize methods and you need to read the java sun tutorial on how to use GroupLayout. . it has a lot of helpful ways to design components so that dynamic updates are possible.

commented: Very good and quick answer +1

THANK YOU!

It was quick and efficient. Bless you.

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.