tags:

views:

41

answers:

1

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.

A: 

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.

Chris Dennett