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