如何在3秒后重新激活图标

时间:2013-10-21 00:07:01

标签: java eclipse eclipse-plugin eclipse-rcp

我有一个RCP GUI,我想在其中做一个图标"刷新"当用户点击它时,它被禁用。 3秒后再次启用。

我想找到一种用线程做的方法。

我遵循了这个教程: Enabling/Disabling Toolbar-Command based on selection in ViewPart, not Perspective 它有效但我的问题是我没有看到如何让eclipse重新激活按钮,而不用TimerThread ......

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果没有线程,你可以在while循环中执行:

long start = System.currentTimeMillis();
long current = start;
long threeSecondsAsMillis = 3 * 1000;
long oneSecondsAsMillis = 1000;

while (current < start + threeSecondsAsMillis) {
    Thread.sleep(oneSecondsAsMillis);
    current += oneSecondsAsMillis;
}
// continue your work

请注意,这是一个糟糕的解决方案,因为它会阻止您的用户界面。更好的解决方案是使用Eclipse Job:

    Job job = new Job("") {

                @Override
                protected IStatus run(IProgressMonitor monitor) {
                    // note, that you are not in the UI-Thread anymore, but you must enable icon in the UI-Thread:


    Display.getDefault().asyncExec(new Runnable() {

            @Override
            public void run() {
            // enable icon
            }
        });
        return Status.OK_STATUS;
 }
 };
job.schedule(3000);

如greg-449所述,您还可以使用UIJob:

UIJob job = new UIJob("") {

                @Override
                    public IStatus runInUIThread(IProgressMonitor monitor) {


    // enable icon
                    return Status.OK_STATUS;
                }
            };
            job.schedule(3000);