Add method in JPanel created using GUI builder

I have GUI created using netbeans GUI builder. I want to add there an object (let's try with button) after pressing a JButton

The very simple code which I wrote in ActionListener and works:

button1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            panel2.add(new JButton("X"));
            panel2.validate();
        }
    });

Howewer in Gui created by netbeans this doesnt work:

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
 jPanel1.add(new JButton("X"));
 jPanel1.validate();
 System.out.print("aa"); 
 }

But it prints "aa" so action listener works.

It looks like a similar problem to one showed here: http://bytes.com/topic/java/answers/857720-adding-icon-jpanel

but I can't see even that rectangle about which JosAH wrote.

I'll appreciate any suggestion.

Answers


Create a placeholder panel in your editor, and then add the panel to that. I think the problem is to do with layouts and such, since some layouts require some layout data to be present on adding the component (i.e., the second field in add(c, ...) is set). So, create a placeholder JPanel using the GUI creator tool, name it so you have a reference to it, and then add the component to that panel. Give it a layout such as FlowLayout which requires no layout data. You may also want to remove all the insets on the panel so you don't get massive spacing, which you can probably do in the UI editor.


Need Your Help

mysql: Query to find matches friends by custom search

php jquery mysql mysqli

I want to find matches friends by example ( city | age | gender | etc )

Externalized time dimension in MySQL OLAP cubes?

mysql olap

It appears to be a common practice to let the time dimension of OLAP cubes be in a table of its own, like the other dimensions.