我已經添加上文本字段修改監聽器,當我嘗試打開它觸發的修改監聽事件,並給出了對話框打開修改監聽器上的文本字段
這裏之前,空指針異常的對話是我的代碼
UserNameDialog dialog = new UserNameDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), true);
dialog.init("", userSession.hasAdminRights(), false, true, false, "");
dialog.open();
if (dialog.getReturnCode() == Window.CANCEL) {
return;
}
對話框類
public class UserNameDialog extends TitleAreaDialog {
private Text txtUsername;
@Override
protected Control createDialogArea(Composite parent) {
setMessage("Enter user information and press OK");
setTitle("User Information");
Composite area = (Composite) super.createDialogArea(parent);
Composite container = new Composite(area, SWT.NONE);
container.setLayout(new GridLayout(2, false));
container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
Label lblUserName = new Label(container, SWT.NONE);
lblUserName.setText("User name");
txtUsername = new Text(container, SWT.BORDER);
txtUsername.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
txtUsername.addListener(SWT.Modify, new Listener()
{
@Override
public void handleEvent(Event event)
{
getButton(IDialogConstants.OK_ID).setEnabled(isValidUsername());
// As I try to open the dialog the handler comes here and throws exception on
// getButton(IDialogConstants.OK_ID)
}
});
if(newUser || currentUserisAdmin)
txtUsername.setEditable(true);
else
txtUsername.setEditable(false);
txtUsername.setText(name);
new Label(container, SWT.NONE);
new Label(container, SWT.NONE);
return area;
}
/**
* Create contents of the button bar.
* @param parent
*/
@Override
protected void createButtonsForButtonBar(Composite parent) {
okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
okButton.setEnabled(false);
}
private boolean isValidUsername()
{
String username = txtUsername.getText();
return username != null && username.trim().length() > 0;
}
}
這是一個對話框內,但其創造的對話框我想之前調用因爲okButton是空
你的代碼看起來就好了。你確定這個監聽器沒有被調用,或者'''''''因爲你的'if'檢查而沒有調用'enableOkButton'嗎? – Baz 2014-11-04 13:34:45
它很好,但我無法在文本字段上添加ModifyListener,它給了我空指針異常 – wearybands 2014-11-04 13:49:55
您必須發佈完成它的代碼。 – Baz 2014-11-04 13:50:44