编程语言
首页 > 编程语言> > java-如何在按OK按钮的TitleAreaDialog中存储数据?

java-如何在按OK按钮的TitleAreaDialog中存储数据?

作者:互联网

为什么在以下代码中(属于扩展TitleAreaDialog的类的一部分):

@Override  
protected void createButtonsForButtonBar(Composite parent) {          
    super.createButtonsForButtonBar(parent);  
    this.getButton(IDialogConstants.OK_ID).addSelectionListener(new SelectionAdapter() {  
        @Override  
        public void widgetSelected(SelectionEvent e) {  
            okPressed();  
        }  
    });  
}  

@Override  
protected void okPressed() {  
    saveInput();  
    super.okPressed();  
}

private void saveInput(){  
    firstNameSelected = firstNameCombo.getText();  
    lastNameSelected = lastNameCombo.getText();      
}    

当我按“确定”按钮时,出现以下异常:

org.eclipse.swt.SWTException: Widget is disposed at
org.eclipse.swt.SWT.error(SWT.java:4361) at
org.eclipse.swt.SWT.error(SWT.java:4276) at
org.eclipse.swt.SWT.error(SWT.java:4247) at
org.eclipse.swt.widgets.Widget.error(Widget.java:468) at
org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:340) at
org.eclipse.swt.widgets.Combo.getText(Combo.java:1006)

在一行中:firstNameSelected = firstNameCombo.getText();的saveInput吗?
为什么将小部件放在选择中?

解决方法:

尝试完全删除createButtonsForButtonBar(Composite parent)方法.该对话框应自行调用okPressed.

而且,我认为没有必要调用super.okPressed().至少我从不使用它.只需调用this.close()即可.

这是我使用的简单模板:

public class OptionsDialog extends Dialog {

    public OptionsDialog(Shell parentShell)
    {
        super(parentShell);
        setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
        setBlockOnOpen(true);
    }

    protected Control createDialogArea(Composite parent) {
        Composite composite = (Composite) super.createDialogArea(parent);

        GridLayout layout = new GridLayout(1, false);
        layout.marginHeight = 5;
        layout.marginWidth = 10;

        composite.setLayout(layout);

        GridData gridData = new GridData();
        gridData.widthHint = 500;

        composite.setLayoutData(gridData);

        createContent();

        return composite;
    }

    private void createContent()
    {
        /* ADD WIDGETS */
    }

    protected void configureShell(Shell newShell)
    {
        super.configureShell(newShell);
        newShell.setText("Options");
    }

    public void okPressed()
    {
        /* SAVE VALUES */
        this.close();
    }

    /* GETTERS AND SETTERS/ */

}

标签:jface,eclipse,eclipse-rcp,swt,java
来源: https://codeday.me/bug/20191127/2075097.html