开发者

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜