I am encountering a situation where Designer is generating code that has compile errors. I have a simple window with a label, text and button using FormLayout. The createContents() method looks like this -
protected void createContents() {
shell = new Shell();
shell.setLayout(new FormLayout());
shell.setSize(500, 375);
shell.setText("SWT Application");
final Label label = new Label(shell, SWT.NONE);
final FormData formData = new FormData();
formData.bottom = new FormAttachment(0, 144);
formData.top = new FormAttachment(0, 131);
formData.left = new FormAttachment(0, 200);
formData.right = new FormAttachment(0, 225);
label.setLayoutData(formData);
label.setText("Label");
final Button button = new Button(shell, SWT.NONE);
final FormData formData_2 = new FormData();
formData_2.top = new FormAttachment(0, 290);
formData_2.left = new FormAttachment(0, 240);
button.setLayoutData(formData_2);
button.setText("button");
text = new Text(shell, SWT.BORDER);
final FormData formData_1 = new FormData();
formData_1.top = new FormAttachment(label, 0, SWT.TOP);
formData_1.left = new FormAttachment(0, 265);
text.setLayoutData(formData_1);
//
}
I have moved some code around - all of the form data code to the end of the createContents() method -
protected void createContents() {
shell = new Shell();
shell.setLayout(new FormLayout());
shell.setSize(500, 375);
shell.setText("SWT Application");
final Label label = new Label(shell, SWT.NONE);
final Button button = new Button(shell, SWT.NONE);
text = new Text(shell, SWT.BORDER);
//
final FormData formData = new FormData();
formData.bottom = new FormAttachment(0, 144);
formData.top = new FormAttachment(0, 131);
formData.left = new FormAttachment(0, 200);
formData.right = new FormAttachment(0, 225);
label.setLayoutData(formData);
label.setText("Label");
final FormData formData_2 = new FormData();
formData_2.top = new FormAttachment(0, 290);
formData_2.left = new FormAttachment(0, 240);
button.setLayoutData(formData_2);
button.setText("button");
final FormData formData_1 = new FormData();
formData_1.top = new FormAttachment(label, 0, SWT.TOP);
formData_1.left = new FormAttachment(0, 265);
text.setLayoutData(formData_1); }
Then I move the button up closer to the text field, which causes it to become attached to that text field. The generated code now looks like -
protected void createContents() {
shell = new Shell();
shell.setLayout(new FormLayout());
shell.setSize(500, 375);
shell.setText("SWT Application");
final Label label = new Label(shell, SWT.NONE);
final Button button = new Button(shell, SWT.NONE);
text = new Text(shell, SWT.BORDER);
formData_2.left = new FormAttachment(text, -49, SWT.LEFT);
formData_2.right = new FormAttachment(text, -5, SWT.LEFT);
//
final FormData formData = new FormData();
formData.bottom = new FormAttachment(0, 144);
formData.top = new FormAttachment(0, 131);
formData.left = new FormAttachment(0, 200);
formData.right = new FormAttachment(0, 225);
label.setLayoutData(formData);
label.setText("Label");
FormData formData_2;
formData_2 = new FormData();
formData_2.top = new FormAttachment(0, 157);
formData_2.bottom = new FormAttachment(0, 180);
button.setLayoutData(formData_2);
button.setText("button");
final FormData formData_1 = new FormData();
formData_1.top = new FormAttachment(label, 0, SWT.TOP);
formData_1.left = new FormAttachment(0, 265);
text.setLayoutData(formData_1);
}
formData_2.left and formData_2.right are now being referenced before formData_2 is defined. Is this a problem with how Designer works or is it invalid to move the form data code after all of the widgets have been defined?
Thanks for any help or suggestions.
Mark