Mixed alignment with Java Swing's GroupLayout
Asked Answered
G

3

8

I'm trying to build a GUI window in my application. What I'm trying to do is have a window, with a few buttons at the top, and a large text area. Something like this:

+--------------------------------------------------+
| [button1] [button2]                    [button3] |
| +----------------------------------------------+ |
| | text area                                    | |
| |                                              | |
| |                                              | |
| |                                              | |
| +----------------------------------------------+ |
+--------------------------------------------------+

I'm almost there, using GroupLayout:

  layout.setHorizontalGroup(
    layout.createParallelGroup()
      .addGroup(layout.createSequentialGroup()
        .addComponent(button1)
        .addComponent(button2))
        .addComponent(closeWindow))
      .addComponent(textarea1)
  );

  layout.setVerticalGroup(
    layout.createSequentialGroup()
      .addGroup(layout.createParallelGroup()
        .addComponent(button1)
        .addComponent(button2)
        .addComponent(button3))
      .addComponent(textarea)
  );

The problem is that this ends up with button3 aligned to the left, with the other two. I can't seem to figure out how I can specify the alignment on just that one button. I can do GroupLayout.Alignment.TRAILING on the entire button bar, but that hits all 3 buttons, which is also not quite right.

So what's the correct approach? Since the alignment only applies for Parallel Groups, I don't think having a HorizontalGroup with two Sequential Groups in it will help?

What am I missing?

Glendon answered 19/4, 2010 at 19:51 Comment(0)
T
11

Add a gap in your sequential group. Leaving your horizontal group as is:

layout.setVerticalGroup(
    layout.createSequentialGroup()
      .addGroup(layout.createParallelGroup()
        .addComponent(button1)
        .addComponent(button2)
        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addComponent(button3))
      .addComponent(textarea)
  );

The gap with those paramters acts as a "spring", taking up all available space.

Trifling answered 19/4, 2010 at 20:22 Comment(2)
Should be "LayoutStyle.ComponentPlacement.RELATED", but other than that, works great, thanks :)Glendon
Hi, should you only add that line to the vertical group or also to the horizontal group?Pithos
M
3

Try adding:

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 1, Short.MAX_VALUE)

after the second button. The MAX_VALUE will cause the gap to expand as much as necessary.

Mcdonough answered 19/4, 2010 at 20:20 Comment(0)
L
1

You want to use addPreferredGap() which is only available on sequential groups. The code below gives you the desired layout.

    layout.setHorizontalGroup(
            layout.createParallelGroup()
                    .addGroup( layout.createSequentialGroup()
                            .addComponent( button1 )
                            .addComponent( button2 )
                            .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE )
                            .addComponent( button3 ) )
                    .addComponent( textArea )
    );
    layout.setVerticalGroup(
            layout.createSequentialGroup()
                    .addGroup( layout.createParallelGroup()
                            .addComponent( button1 )
                            .addComponent( button2 )
                            .addComponent( button3 ) )
                    .addComponent( textArea )
    );
Lukash answered 27/8, 2014 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.