GroupLayout autogenerated code in NetBeans

3k views Asked by At

After almost 4 years in java programming I decided to learn how to write GUI classes by myself since until now I've always used NetBeans GUI Editor (I'm not particularly proud of it but it has worked pretty well avoiding me worry about the components layout).

The thing is I'm following How to Use GroupLayout tutorial to learn about this layout manager that I find very powerful. Now I made a little example by my own and then try to do the same in Netbeans GUI Editor and I found some differences between both codes and I'd like to know if I'm missing something or NetBeans just adds useless code in GroupLayout definition.

This is my target:

enter image description here

This is my SSCCE:

public static void main(String[] args) {        
    JLabel label = new JLabel("This is a test");
    label.setFont(new Font("Segoe UI Semibold", Font.BOLD | Font.ITALIC, 24));

    JSeparator separator = new JSeparator(JSeparator.HORIZONTAL);

    DefaultListModel model = new DefaultListModel();
    model.addElement("Apple");
    model.addElement("Orange");
    model.addElement("Kiwi");
    model.addElement("Watermelon");

    JList list = new JList(model);
    list.setPreferredSize(new Dimension(400, 300));
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(list);

    JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


    Container contentPane = frame.getContentPane();        
    GroupLayout layout = new GroupLayout(contentPane);
    layout.setAutoCreateContainerGaps(true);
    contentPane.setLayout(layout);

    layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(label, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
                .addComponent(separator)
                .addComponent(scrollPane)
            );        
    layout.setVerticalGroup(
            layout.createSequentialGroup()
                .addComponent(label)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(separator, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)               
        );        
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

As you can see I have only defined a paralell group as horizontal group and a sequential group as vertical group. But Netbeans autogenerate this code:

    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(label, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
                .addComponent(separator)
                .addComponent(scrollPane))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(label)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(separator, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
            .addContainerGap())
    );

As you can see the group structure is a little more complex than mine. I just want to know if I'm mistaken or Netbeans just unnecessarily adds more groups than needed.

1

There are 1 answers

1
trashgod On BEST ANSWER

Kudos for embracing the NetBeans GUI designer as a means to—and not a substitute for—understanding Swing. Summarizing the comments,

  • While GroupLayout was designed for automated code generation, it can usefully be used manually, as shown here and here. It can also be integrated into the mixed development approach suggested here.

  • Experienced developers wisely suggest learning one or more popular third-party layouts such as MigLayout, FormLayout or DesignGridLayout, which derive some power from accepting human-readable text parameters. I see GroupLayout in the same category, but simply having a fluent interface.

  • In your example, the two layouts have differing resize behavior, which may impact other choices. Beware of this common pitfall.