Search code examples
javauser-interfaceswtgrid-layout

SWT- equal weights to GridLayout row elements?


I'm trying to add 7 Text widgets to a GridLayout row. I want them all to be of the same width, but they come out funky:

enter image description here

//Columns
    for(int i=0;i<7;i++) {
        text = new Text(shell, SWT.BORDER);
        text.setEditable(true);
        data = new GridData(SWT.FILL, SWT.TOP,true,false,1,1);
        text.setLayoutData(data);
        cols.add(text);
    }

Things I've tried:

  • Almost any combination of FILL/TOP and true/false to the GridData constructor (I was desperate).
  • Setting data.minimumWidth and data.widthHint to (window width)/7.

Yet the widgets are always in varying degrees of disarray.


Solution

  • You specify this on the GridLayout constructor, second parameter:

    GridLayout layout = new GridLayout(7, true);
    

    specifies 7 columns of equal width.

    You can also use

    layout.makeColumnsEqualWidth = true;