1212import org .eclipse .swt .widgets .Composite ;
1313import org .eclipse .swt .widgets .Control ;
1414import org .eclipse .swt .widgets .Label ;
15+ import org .eclipse .swt .widgets .Layout ;
1516import org .eclipse .swt .widgets .Shell ;
1617import org .eclipse .swt .widgets .Text ;
1718
1819public class MyDialog extends TitleAreaDialog {
1920
20- private Text firstNameText ;
21+ private Text txtFirstName ;
2122 private Text lastNameText ;
2223 private String firstName ;
2324 private String lastName ;
@@ -38,55 +39,43 @@ public void create() {
3839
3940 @ Override
4041 protected Control createDialogArea (Composite parent ) {
41- GridLayout layout = new GridLayout ( );
42- layout . numColumns = 2 ;
43- // layout.horizontalAlignment = GridData.FILL ;
44- parent . setLayout ( layout );
45-
46- // The text fields will grow with the size of the dialog
47- GridData gridData = new GridData ( );
48- gridData . grabExcessHorizontalSpace = true ;
49- gridData . horizontalAlignment = GridData . FILL ;
50-
51- Label label1 = new Label ( parent , SWT . NONE ) ;
52- label1 . setText ( "First Name" );
53-
54- firstNameText = new Text ( parent , SWT . BORDER );
55- firstNameText . setLayoutData ( gridData );
56-
57- Label label2 = new Label ( parent , SWT . NONE );
58- label2 . setText ( "Last Name" );
42+ Composite composite = ( Composite ) super . createDialogArea ( parent );
43+ Composite area = new Composite ( composite , SWT . NONE ) ;
44+ GridLayout layout = new GridLayout ( 2 , false ) ;
45+ area . setLayoutData ( new GridData ( SWT . FILL , SWT . FILL , true , true ) );
46+ area . setLayout ( layout );
47+ Label lbtFirstName = new Label ( area , SWT . NONE );
48+ lbtFirstName . setText ( "First Name" );
49+
50+ GridData dataFirstName = new GridData () ;
51+ dataFirstName . grabExcessHorizontalSpace = true ;
52+ dataFirstName . horizontalAlignment = GridData . FILL ;
53+
54+ txtFirstName = new Text ( area , SWT . BORDER );
55+ txtFirstName . setLayoutData ( dataFirstName );
56+
57+ Label lbtLastName = new Label ( area , SWT . NONE );
58+ lbtLastName . setText ( "Last Name" );
59+
5960 // You should not re-use GridData
60- gridData = new GridData ();
61- gridData .grabExcessHorizontalSpace = true ;
62- gridData .horizontalAlignment = GridData .FILL ;
63- lastNameText = new Text (parent , SWT .BORDER );
64- lastNameText .setLayoutData (gridData );
65- return parent ;
61+ GridData dataLastName = new GridData ();
62+ dataLastName .grabExcessHorizontalSpace = true ;
63+ dataLastName .horizontalAlignment = GridData .FILL ;
64+ lastNameText = new Text (area , SWT .BORDER );
65+ lastNameText .setLayoutData (dataLastName );
66+ return area ;
6667 }
6768
68- private boolean isValidInput () {
69- boolean valid = true ;
70- if (firstNameText .getText ().length () == 0 ) {
71- setErrorMessage ("Please enter the first name" );
72- valid = false ;
73- }
74- if (lastNameText .getText ().length () == 0 ) {
75- setErrorMessage ("Please enter the last name" );
76- valid = false ;
77- }
78- return valid ;
79- }
80-
8169 @ Override
8270 protected boolean isResizable () {
8371 return true ;
8472 }
8573
86- // We need to have the textFields into Strings because the UI gets disposed
87- // and the Text Fields are not accessible any more.
74+ // We need to save the values of the Text fields into Strings because the UI
75+ // gets disposed
76+ // and the Text fields are not accessible any more.
8877 private void saveInput () {
89- firstName = firstNameText .getText ();
78+ firstName = txtFirstName .getText ();
9079 lastName = lastNameText .getText ();
9180
9281 }
0 commit comments