Community
Participate
Working Groups
The horizontal border lines drawn around the Text control generated in a Form context is off by one compared to the vertical lines. When adding a Text Control to fit into a Composite using a GridLayout, you must set margins in the GridLayout like this for the border to appear correctly: gridLayout.marginWidth = 1; gridLayout.marginHeight = 2; It appears to me that the value shsould be the same for both margins values. Here is sample code that reproduces this problem. The code uses "1" in both margins. Change the height margin to 2 so see the borders correcly. ----------------------------------------- import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.forms.widgets.FormToolkit; public class FormCompositeWithText extends Composite { private Text text; public FormCompositeWithText(Composite parent, int style) { super(parent, style); FormToolkit toolkit = new FormToolkit(parent.getDisplay()); toolkit.adapt(this); toolkit.paintBordersFor(this); final GridLayout gridLayout = new GridLayout(); gridLayout.horizontalSpacing = 0; gridLayout.verticalSpacing = 0; gridLayout.marginWidth = 1; gridLayout.marginHeight = 1; //gridLayout.marginHeight = 2; // Use this value for correctly drawn border setLayout(gridLayout); text = toolkit.createText(this, null, SWT.NONE); text.setLayoutData(new GridData(GridData.FILL_BOTH)); text.setText("New Forms Text"); // } public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setSize(800, 600); Composite c = new FormCompositeWithText(shell, SWT.NONE); c.setBounds(10, 10, 150, 20); shell.open(); while (!shell.isDisposed()) if (!display.readAndDispatch()) display.sleep(); display.dispose(); } }
It works as designed (but perhaps it is not documented well). When borders are drawn for the Text control, padding is added vertically around the widget. Otherwise, the widget looks too short. If you check 'Text.computeTrim' method, you will notice that they do exactly the same thing if SWT.BORDER style is set. Your solution is what we do everywhere in Eclipse - ensure that there are 2 pixels vertical margin in a parent of the Text control when used in a form.