3

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.

dic19
  • 17,821
  • 6
  • 40
  • 69
  • 4
    Just a comment: GroupLayout is designed to be used with a builder, so it's hard to use it with manual coding. My suggestion would be to use a LayoutManager that is designed to be _easily_ usable in manual code, fi one of the Big Three: MigLayout, FormLayout, DesignGridBagLayout. – kleopatra Sep 11 '13 at 15:39
  • 1
    +1 for sscce; note, the two have different resize behavior. – trashgod Sep 11 '13 at 15:42
  • Thanks for the comments! I'll take a look to other layout managers because, yes, it's hard to use this by hand coding. @trashgod you're right about resize behavior. I didn't note that before. Particularly I don't like allow resizing very much unless it be strictly necessary f.e. big tables or something that could need much space. That's just my preference. – dic19 Sep 11 '13 at 18:08
  • @kleopatra DesignGridLayout. GridBagLayout must have accidentally sneaked in, though I must say that even that one is easy to use manually compared to GroupLayout. – kiheru Sep 11 '13 at 19:09
  • @kiheru: I _wondered_ about that. :-) – trashgod Sep 11 '13 at 19:19
  • @kiheru outch .. yeah, meant DesignGrid (GridBag is an unusable monstrosity ;-) Thanks for the heads up! – kleopatra Sep 12 '13 at 07:04

1 Answers1

6

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    Thanks a lot for your time. I've got a lot to read here and it's very useful. Especially the tip about resizing ;) – dic19 Sep 11 '13 at 19:32