0

I am wanting to make each cell in a row a different length.. Here is a picture to help.

So the Policy cell and the text to the right is fine. However, Section 1 and Section 2 I want to be 50/50.. Not 20/80 or whatever it is now. I have started using the WindowsBuilder tool instead of doing this by hand. Is this possible to do?

Community
  • 1
  • 1
Chris Bolton
  • 2,162
  • 4
  • 36
  • 75
  • Is this SWT or Swing - both have (different) GridLayouts? This can certainly be done using multiple levels of GridLayout in SWT. – greg-449 Nov 25 '14 at 14:31

2 Answers2

1

To lay out controls in the requested manner with a GridLayout in SWT you will have to group the controls of each row into a composite of their own like so:

shell.setLayout( new RowLayout( SWT.VERTICAL ) );
Composite composite1 = new Composite( shell, SWT.NONE );
composite1.setLayout( new GridLayout( 2, false ) );
createLabel( composite1, "2020" );
createLabel( composite1, "808080808080" );

Composite composite2 = new Composite( shell, SWT.NONE );
composite2.setLayout( new GridLayout( 2, false ) );
createLabel( composite2, "50505050" );
createLabel( composite2, "50505050" );

private static Label createLabel( Composite parent, String text ) {
  Label label = new Label( parent, SWT.NONE );
  label.setText( text );
  return label;
}

However, to me a FormLayout seems more suitable to solve the given problem:

shell.setLayout( new FormLayout() );
FormData leftFormData = new FormData();
leftFormData.top = new FormAttachment( 0 );
leftFormData.left = new FormAttachment( 0 );
leftFormData.right = new FormAttachment( 20 );
Label leftLabel = createLabel( shell, "2020", leftFormData );
FormData rightFormData = new FormData();
rightFormData.top = new FormAttachment( 0 );
rightFormData.left = new FormAttachment( leftLabel );
rightFormData.right = new FormAttachment( 100 );
createLabel( shell, "808080808080", rightFormData );

private static Label createLabel( Composite parent, String text, Object layoutData ) {
  Label label = new Label( parent, SWT.NONE );
  label.setText( text );
  label.setLayoutData( layoutData );
  return label;
}

If you find the formData and formAttachment code too verbose, you may have a look at Slim Down SWT FormLayout Usage

And for an in-depth discussion of SWT layouts I recommend the Understanding Layouts in SWT article.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • Thank you! I actually like using the grid layout inside the row layout. Now I just need the RowData to span the size of the screen. – Chris Bolton Nov 25 '14 at 18:57
  • Be aware that using the `GridLayout` _wastes_ an extra `Composite` per row and might lead to performacne issues when there are many rows. To span the size of the parent, you can use a `FillLayout` or a single-row `GridLayout` with `GridData`s like `new GridData( SWT.FILL, SWT.TOP, true, false )`. to take all the horizontal space. – Rüdiger Herrmann Nov 26 '14 at 08:56
  • My issue is really here: composite.setLayoutData(new RowData(622, 34)); I really want RowData to span the screen and not be a set size. – Chris Bolton Nov 26 '14 at 12:53
  • No sure what you mean by _screen_. If you want a control to span its entire parent, a RowLayout is the wrong choice. See my above comment that suggests `FillLayout` or `GridLayout`. – Rüdiger Herrmann Nov 26 '14 at 13:50
0

EDIT: Missed that you are using SWT GridLayout instead of the Swing one. My solution works with Swing so keep that in mind.

You may want to use the GridBagLayout. It's more complicated than the GridLayout but offers greater flexibility

See some documentation on how to use it here

Liam de Haas
  • 1,258
  • 3
  • 18
  • 39
  • Is this not possible with the normal GridLayout? – Chris Bolton Nov 25 '14 at 14:49
  • @ChrisBolton If you look [here](http://stackoverflow.com/questions/6471502/how-can-i-make-my-columns-different-sizes-using-gridlayout-in-swing) you can see in the answer it is possible with a combination of `FlowLayout` and `GridLayout` but that makes it very tedious and very complicated. With `GridBagLayout` it's still tedious but not as much. I Strongly suggest using `GridBagLayout` because its the most flexible option. And once you understand it it becomes less tedious. **tl;dr: No.** – Liam de Haas Nov 25 '14 at 14:54
  • GridBagLayout is AWT/Swing, the question is about SWT. – greg-449 Nov 25 '14 at 14:58
  • @greg-449 Missed that part – Liam de Haas Nov 25 '14 at 14:59
  • Yeah, I believe the best solution is to use FlowLayout with GridLayout. Then, I can stick with SWT. – Chris Bolton Nov 25 '14 at 15:04