GroupLayout Auto Generated Code in NetBeans

After almost 4 years in Java programming, I decided to learn how to write GUI classes myself, since so far I have always used the editor of the NetBeans graphical editor (I'm not particularly proud of this, but it worked pretty well, avoiding me about component layout).

The fact is that I am following How to use the GroupLayout Guide to find out about this layout manager, which I find very powerful. Now I made a small example of my own, and then try to do the same in the Netbeans graphical editor, and find some differences between the two codes, and I would like to know that I missed something, or NetBeans just adds useless code to GroupLayout definition.

It is my goal:

enter image description here

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);
}

, paralell . Netbeans :

    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())
    );

, , . , , Netbeans , .

+3
1

GUI NetBeans - Swing. ,

+6

Source: https://habr.com/ru/post/1529076/


All Articles