Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFace对话框在小部件仍在使用时处理小部件_Java_Swt_Jface - Fatal编程技术网

Java JFace对话框在小部件仍在使用时处理小部件

Java JFace对话框在小部件仍在使用时处理小部件,java,swt,jface,Java,Swt,Jface,我有一个类扩展了jface.dialogs.Dialog。该对话框中有一个保存按钮。当用户按下该按钮时,我需要从一些swt.widgets.Text字段中读取值,但是文本字段已经被释放 我做错了什么 public class MyNewDialog extends Dialog { private Text txt; public MyNewDialog(Shell parentShell) { super(parentShell); } @Override protected Co

我有一个类扩展了
jface.dialogs.Dialog
。该对话框中有一个保存按钮。当用户按下该按钮时,我需要从一些
swt.widgets.Text
字段中读取值,但是文本字段已经被释放

我做错了什么

public class MyNewDialog extends Dialog {
private Text txt;

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

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

    txt = new Text(container, SWT.BORDER);
    txt.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));

    return container
}

@Override
protected void createButtonsForButtonBar(Composite parent) {
    Button saveButton = createButton(parent, IDialogConstants.OK_ID, "Save", true);
    saveButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent p_e) {
            String string = txt.getText() //widget is disposed exception
        }
    }
}

由于您正在为按钮使用
IDialogConstants.OK\u ID
,因此可以使用
okPressed()
方法。无需添加特定的侦听器

@Override
protected void okPressed()
{
    value = txt.getText();

    super.okPressed();
}
然后创建一个getter方法来返回
变量:

public String getValue()
{
    return value;
}

谢谢,这很有帮助!当我在一个复合类中时,是否有类似的情况,或者我是否需要在该类中添加侦听器?这是特定于
对话框
类的。如果使用的是
组合
(不在对话框中),则需要将
侦听器
添加到按钮中。