如何在 Eclipse RCP 应用程序中 5 秒后自动隐藏标签?

How to hide a label automatically after 5 seconds in Eclipse RCP application?

我正在尝试在 RCP 应用程序中创建一个最初不可见的标签。当我单击“保存”按钮时,它变得可见。同样,它应该在 5 秒内隐形。

为此,我编写了以下代码:

saveButton.addSelectionListener(new SelectionListener() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                // TODO Auto-generated method stub
                System.out.println(textName.getText());
                String text = textName.getText();
                tree.getSelection()[0].setText(text);
                String nodeId = ((TreeStructure) tree.getSelection()[0]
                        .getData()).getNodeId();
                // update the database
                UpdateTree updateTree = new UpdateTree();
                updateTree.renameNode(text, nodeId);
                label.setBounds(xForFirstButton, yIndexForButtons
                        + Constants.BUTTON_BUFFER, Constants.BUTTON_WIDTH,
                        Constants.BUTTON_HEIGHT);
                label.setVisible(true);

                AbstractAction myAction = new AbstractAction() {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        // TODO Auto-generated method stub
                        label.setVisible(false);
                    }
                };

                Timer myTimer = new Timer(5000, myAction);
                myTimer.start();
            }

但是,这段代码 运行 给出了以下错误:

Exception in thread "AWT-EventQueue-0" org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(SWT.java:4441)
    at org.eclipse.swt.SWT.error(SWT.java:4356)
    at org.eclipse.swt.SWT.error(SWT.java:4327)
    at org.eclipse.swt.widgets.Widget.error(Widget.java:476)
    at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:367)
    at org.eclipse.swt.widgets.Control.setVisible(Control.java:3781)
    at com.app.editor.views.EditorView.actionPerformed(EditorView.java:183)
    at javax.swing.Timer.fireActionPerformed(Unknown Source)
    at javax.swing.Timer$DoPostEvent.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access0(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

谁能指出问题所在?

提前致谢!

要从非 ui 线程的线程更新您 ui,您必须使用此:

Display.getDefault().syncExec(new Runnable() {
     public void run() {
           // Update UI here
     }
});

syncExec 将在您的 rcp 应用程序的 ui 线程中执行您的操作。

这类似于Swing中的invokeLater()方法。

所有更改 UI 的代码必须 运行 在 UI 线程中 - Timer 是 运行 在另一个线程中调用代码。

而不是 Timer 使用:

Display.getDefault().timerExec(milliseconds, runnable);