Reputation: 54094
Why in the following code (part of a class that 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();
}
I get the following exception when I press the OK
button:
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)
In line: firstNameSelected = firstNameCombo.getText();
of saveInput
?
Why is the widget disposed on selection?
Upvotes: 3
Views: 1990
Reputation: 2188
You don't need to call okPressed()
method inside widgetSelected()
. I don't think it is necessary to call widgetSelected()
method at all. The okPressed()
is called by the dialog itself.
You may want to try the following code.
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
Upvotes: 1
Reputation: 36894
Try removing the createButtonsForButtonBar(Composite parent)
method completely. The dialog should call okPressed
by itself.
Moreover, I think calling super.okPressed()
isn't necessary. At least I never use it. Just call this.close()
instead.
Here is the simple template I use:
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/ */
}
Upvotes: 2