Eclipse SWT GridLayout – Making the widget fill horizontally

I have been stuck with the problem of making UI widgets fill horizontally in a grid layout a few times. The solution is simple, but when I have to create UI using Eclipse SWT after a long gap, I tend to forget how I had made this work earlier, so I decided to blog about this.

I had to create a simple UI with one label and associated text box. I coded it like this –

GridLayout layout = new GridLayout(2, false);
tabContainer.setLayout(layout); //this is the parent composite

Label label1 = new Label(tabContainer, SWT.NONE);
label1.setText("Some Label:");
GridData gd = new GridData();
gd.grabExcessHorizontalSpace = false;
label1.setLayoutData(gd);

Text txtBox1 = new Text(tabContainer, SWT.BORDER);
gd = new GridData();
gd.grabExcessHorizontalSpace = true;
txtBox1.setLayoutData(gd);

Continue reading “Eclipse SWT GridLayout – Making the widget fill horizontally”