2015-07-03 111 views
0

我想設計一個使用SWT的密碼對話框,我可以使用下面的代碼來實現這個功能,但是我也想讓這兩個字段都是強制性的,而且我無法理解如何在下面的代碼中包含這個功能。請幫幫我。如何使字段在SWT對話中爲必填字段?

import org.eclipse.jface.dialogs.*; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.events.*; 
import org.eclipse.swt.graphics.Point; 
import org.eclipse.swt.layout.*; 
import org.eclipse.swt.widgets.*; 

public class PasswordDialog extends Dialog { 
    private Text txtUser; 
    private Text txtPassword; 
    private String user = ""; 
    private String password = ""; 

    public PasswordDialog(Shell parentShell) { 
     super(parentShell); 
    } 

    @Override 
    protected Control createDialogArea(Composite parent) { 
     Composite container = (Composite) super.createDialogArea(parent); 
     GridLayout layout = new GridLayout(2, false); 
     layout.marginRight = 5; 
     layout.marginLeft = 10; 
     container.setLayout(layout); 

     Label lblUser = new Label(container, SWT.NONE); 
     lblUser.setText("User:"); 

     txtUser = new Text(container, SWT.BORDER); 
     txtUser.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); 
     txtUser.setText(user); 
     txtUser.addModifyListener(new ModifyListener() { 

      @Override 
      public void modifyText(ModifyEvent e) { 
       Text textWidget = (Text) e.getSource(); 
       String userText = textWidget.getText(); 
       user = userText; 
      } 
     }); 

     Label lblPassword = new Label(container, SWT.NONE); 
     GridData gd_lblNewLabel = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1); 
     gd_lblNewLabel.horizontalIndent = 1; 
     lblPassword.setLayoutData(gd_lblNewLabel); 
     lblPassword.setText("Password:"); 

     txtPassword = new Text(container, SWT.BORDER| SWT.PASSWORD); 
     txtPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); 
     txtPassword.setText(password); 
     txtPassword.addModifyListener(new ModifyListener() { 

      @Override 
      public void modifyText(ModifyEvent e) { 
       Text textWidget = (Text) e.getSource(); 
       String passwordText = textWidget.getText(); 
       password = passwordText; 
      } 
     }); 
     return container; 
    } 

    // override method to use "Login" as label for the OK button 
    @Override 
    protected void createButtonsForButtonBar(Composite parent) { 
     createButton(parent, IDialogConstants.OK_ID, "Login", true); 
     createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); 
    } 

    @Override 
    protected Point getInitialSize() { 
     return new Point(450, 300); 
    } 

    @Override 
    protected void okPressed() { 
     user = txtUser.getText(); 
     password = txtPassword.getText(); 
     super.okPressed(); 
    } 

    public String getUser() { 
     return user; 
    } 

    public void setUser(String user) { 
     this.user = user; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

} 
+0

難道你只是驗證'okPressed'被調用? – Mena

+0

請提供一個簡單的例子,並進一步指定您的問題,這太寬泛了。此外,記錄您的代碼可以更容易閱讀和理解。如果有人不得不低頭理解一個問題,你不可能得到答案。哦,請妥善格式化您的代碼,這也有助於可讀性! – ShellFish

+0

我認爲你可以省略okPressed()方法,因爲你的ModifyListeners已經完成了這項工作。或者,您可以刪除=「」初始化。少一點代碼。 –

回答

0

在您的modifyText聽衆中,您可以啓用/禁用「確定」按鈕。使用類似的東西:

void updateOKStatus(boolean enable) 
{ 
    Button ok = getButton(IDialogConstants.OK_ID); 
    if (ok != null) 
    ok.setEnabled(enable); 
}