Java group layout cancel accept buttons

70 Views Asked by At

I'm trying to use GroupLayout to do something like the following:

-------------------------------
-   label1: aLongTextfield1   -
-   label2: checkbox1         -
-           checkbox2         -
-               accept cancel -
-------------------------------

label1 and label2 have trailing alignment. aLongTextfield1, checkbox1, and checkbox2 have leading alignment and the accept and cancel buttons should be trailing so the cancel button is along the right edge.

Here's the code:

GroupLayout layout = new GroupLayout(this);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
setLayout(layout);

layout.setHorizontalGroup(
        layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                  .addComponent(nameLabel)
                                  .addComponent(evaluatorLabel)
                                  .addComponent(boundingBoxLabel))
                .addGroup(layout.createParallelGroup()
                                  .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                    .addComponent(nameField)
                                                    .addComponent(landCheckBox)
                                                    .addComponent(shallowCheckBox)
                                                    .addComponent(waterCheckBox)
                                                    .addComponent(deepCheckBox)
                                                    .addComponent(boundingBoxArea))
                                  .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                                    .addGroup(layout.createSequentialGroup()
                                                                      .addComponent(createButton)
                                                                      .addComponent(cancelButton)))));

layout.setVerticalGroup(
        layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                  .addComponent(nameLabel)
                                  .addComponent(nameField))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                  .addComponent(evaluatorLabel)
                                  .addComponent(landCheckBox))
                .addComponent(shallowCheckBox)
                .addComponent(waterCheckBox)
                .addComponent(deepCheckBox)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                  .addComponent(boundingBoxLabel)
                                  .addComponent(boundingBoxArea,
                                                GroupLayout.PREFERRED_SIZE,
                                                GroupLayout.DEFAULT_SIZE,
                                                GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                  .addComponent(createButton)
                                  .addComponent(cancelButton)));

// Make the bottom buttons the same size
layout.linkSize(SwingConstants.HORIZONTAL, createButton, cancelButton);

When I run this the accept and cancel buttons are justified along the left edge of the checkboxes, but I want them to be justified along the right edge of the frame. I can solve this by putting the buttons in a separate panel and then using BorderLayout to add them at PAGE_END, but I think there has to be a way to do this all in GroupLayout.

SOLVED

The second group in the horizontal group should have a TRAILING alignment. The button group in the horizontal group should be sequential.

layout.setHorizontalGroup(
  layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
      .addComponent(label1)
      .addComponent(label2))
    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
      .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addComponent(aLongTextField)
        .addComponent(checkBox1)
        .addComponent(checkBox2))
      .addGroup(layout.createSequentialGroup()
        .addComponent(acceptButton)
        .addComponent(cancelButton))));
0

There are 0 best solutions below