为什么在以下代码中(extends 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();
}
按OK
按钮时出现以下异常:
org.eclipse.swt.SWTException:小部件处于 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();
的{{1}}?
为什么小部件处于选择状态?
答案 0 :(得分:2)
尝试完全删除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/ */
}
答案 1 :(得分:1)
您无需在okPressed()
内调用widgetSelected()
方法。我认为根本不需要调用widgetSelected()
方法。对话框本身调用okPressed()
。
您可能需要尝试以下代码。
public ClassNameWhichExtendsTitleAreDialog(Shell parentShell) {
super(parentShell);
}
@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
}
@Override
protected void okPressed() {
firstNameSelected = firstNameCombo.getText();
lastNameSelected = lastNameCombo.getText();
super.okPressed();
}
// A function to return the name values obtained