我在文本字段上添加了一个Modify监听器,当我尝试打开对话框时,它会触发Modify listener事件并在打开对话框之前给出空指针异常
这是我的代码
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为null
答案 0 :(得分:0)
好的,这是一个带有对话框的示例,如果用户名长度至少为一个字符,则该对话框仅启用“确定”按钮:
private Text text;
protected UsernameDialog(Shell parentShell)
{
super(parentShell);
}
@Override
protected void configureShell(Shell newShell)
{
super.configureShell(newShell);
newShell.setText("Dialog");
}
@Override
protected Control createDialogArea(Composite parent)
{
Composite container = (Composite) super.createDialogArea(parent);
text = new Text(container, SWT.BORDER);
text.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
text.addListener(SWT.Modify, new Listener()
{
@Override
public void handleEvent(Event event)
{
getButton(IDialogConstants.OK_ID).setEnabled(isValidUsername());
}
});
return container;
}
private boolean isValidUsername()
{
String username = text.getText();
return username != null && username.trim().length() > 0;
}
@Override
protected void okPressed()
{
if (isValidUsername())
super.okPressed();
else
System.out.println("Invalid username");
}
@Override
protected void createButtonsForButtonBar(Composite parent)
{
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
getButton(IDialogConstants.OK_ID).setEnabled(false);
}
public static void main(String[] args)
{
new UsernameDialog(new Shell()).open();
}
答案 1 :(得分:0)
首先感谢Baz的帮助。我发布了所有相关代码,但我无法弄清楚为什么你的解决方案不起作用。我为我工作和工作的内容如下:
txtUsername.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
updateOKButton();
}
});
@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 void updateOKButton() {
if(newUser){
if(txtUsername.getText() == null || txtUsername.getText().isEmpty()){
this.setMessage("Username can not be empty!");
okButton.setEnabled(false);
}
else if(txtPassword.getText().isEmpty() && txtPassword_1.getText().isEmpty()){
this.setMessage("Passwords cannot be null!");
okButton.setEnabled(false);
}
else if (!(txtPassword.getText().equals(txtPassword_1.getText()))) {
this.setMessage("Password do not match!");
okButton.setEnabled(false);
}else {
this.setMessage(null);
okButton.setEnabled(true);
}
}
else{
if(txtUsername.getText() == null || txtUsername.getText().isEmpty()){
this.setMessage("Username can not be empty!");
okButton.setEnabled(false);
}
else {
this.setMessage(null);
okButton.setEnabled(true);
}
}
}
由于