Java SWTException:尝试从向导页面的面板中调用updateButtons()时,线程访问无效

Java SWTException:尝试从向导页面的面板中调用updateButtons()时,线程访问无效,java,multithreading,Java,Multithreading,我需要启用/禁用向导对话框上的完成按钮,具体取决于向导页面中面板中的某个字段 以下是打开向导对话框的代码: UserWizardDialog dialog = new UserWizardDialog(window.getShell(), new UserWizard(window.getShell())); dialog.open(); 在这个UserWizard中,有一个名为CustomerWizardPage的WizardPage,它有一个CustomerPanel。在此面板中,我有Cu

我需要启用/禁用
向导对话框
上的
完成
按钮,具体取决于
向导页面
中面板中的某个字段

以下是打开向导对话框的代码:

UserWizardDialog dialog = new UserWizardDialog(window.getShell(), new UserWizard(window.getShell()));
dialog.open();
在这个
UserWizard
中,有一个名为
CustomerWizardPage
WizardPage
,它有一个
CustomerPanel
。在此面板中,我有Customer
PIN
字段,根据其值,我必须启用/禁用
UserWizardDialog
上的
Finish
按钮

在面板中该字段的
itemstener
事件中,我添加了以下代码:

parent.getWizard().getContainer().updateButtons();
但它给出了一个例外:

org.eclipse.swt.SWTException: Invalid thread access
at org.eclipse.swt.SWT.error(SWT.java:3884)
at org.eclipse.swt.SWT.error(SWT.java:3799)
at org.eclipse.swt.SWT.error(SWT.java:3770)
at org.eclipse.swt.widgets.Widget.error(Widget.java:463)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:355)
at org.eclipse.swt.widgets.Control.setEnabled(Control.java:2923)
at org.eclipse.jface.wizard.WizardDialog.updateButtons(WizardDialog.java:1257)
at com.noi.rac.dhl.eco.util.components.CustomerPanel$4.itemStateChanged(CustomerPanel.java:304)
在非UI线程中修改UI组件时会发生“无效线程访问”

要避免此异常,应在UI线程中修改UI组件。在源代码中,我认为应该更新UI线程中的按钮

例如:

Display.getDefault().asyncExec(new Runnable() {
    @Override
    public void run() {
        parent.getWizard().getContainer().updateButtons();
    }
});
您可以根据需要使用syncExec()或asyncExec()

希望这对您有所帮助。

当您在非UI线程中修改UI组件时,会发生“无效线程访问”

要避免此异常,应在UI线程中修改UI组件。在源代码中,我认为应该更新UI线程中的按钮

例如:

Display.getDefault().asyncExec(new Runnable() {
    @Override
    public void run() {
        parent.getWizard().getContainer().updateButtons();
    }
});
您可以根据需要使用syncExec()或asyncExec()

希望这对你有帮助