user2129160
user2129160

Reputation: 89

Netbeans GUI add buttons using if statement

I am creating a Java GUI using Netbeans GUI creator.

And I want to add buttons to a panel using a if statement, ive done this many times before, but not with Netbeans.

for (int i = 0; i < list.length; i++){
                          if (something){
                              String files = something.getName();
                              JButton btn = new JButton(files);
                              panel.add(btn);
                              panel.validate();

This is the if statement which worked when I didn't use Netbeans GUI maker. But now nothing is displayed, and no errors appear.

this is how the panel is created

public final javax.swing.JPanel panel = new javax.swing.JPanel();

       javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
    panel.setLayout(panelLayout);
    panelLayout.setHorizontalGroup(
        panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );
    panelLayout.setVerticalGroup(
        panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );

Anyone have any ideas??

Upvotes: 1

Views: 705

Answers (1)

Reimeus
Reimeus

Reputation: 159844

GroupLayout requires that any new buttons/comooinents be added using the correct horizontal & vertical groups. Alternatively, you can use a different layout manager which does not require any constraints to be set on newly added components. These include FlowLayout and GridLayout.

Upvotes: 1

Related Questions