简体   繁体   English

SWT中的javax.swing.Timer等效吗?

[英]javax.swing.Timer equivalent in SWT?

I need some means to run code on user-interface thread with delay and with ability to abort waiting. 我需要一些方法来在用户界面线程上运行代码,并具有延迟和中止等待的能力。 This can be done by javax.swing.Timer in Swing . 这可以通过Swingjavax.swing.Timer完成。 Is there similar functionality in SWT ? SWT有类似的功能吗? Display#timerExec has no apparent ability to cancel future running. Display#timerExec没有明显的能力来取消将来的运行。

Here is an example using Display#timerExec(int, Runnable) : 这是使用Display#timerExec(int, Runnable)的示例:

public static void main(String[] args)
{
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new FillLayout(SWT.VERTICAL));

    final Text text = new Text(shell, SWT.BORDER);

    final Button runButton = new Button(shell, SWT.CHECK);
    runButton.setText("Stop");

    final Runnable run = new Runnable()
    {
        private int counter = 0;
        @Override
        public void run()
        {
            if(runButton.getSelection())
                return;

            text.setText(Integer.toString(counter++));

            display.timerExec(1000, this);
        }
    };

    display.timerExec(1000, run);

    runButton.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            if(!runButton.getSelection())
                display.timerExec(1000, run);
        }
    });

    shell.pack();
    shell.setSize(200, shell.getSize().y);
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

My goal was to make idle detector to smooth user interface. 我的目标是使闲置检测器平滑用户界面。 When user makes changes in one part of UI, then the system waits for 1000 ms and updates other parts of UI. 当用户在用户界面的一部分中进行更改时,系统将等待1000毫秒并更新用户界面的其他部分。 If during the waiting, say in 800 ms, user makes changes again, then system cancels waiting for that period and start to wait for 1000 ms again. 如果在等待期间(例如在800毫秒内),用户再次进行更改,则系统取消等待该时间段并开始再次等待1000毫秒。

In Swing this was solved with one-time delayed timer. Swing ,使用了一个延时计时器解决了这一问题。 Swing's Timer made good for this. Swing的计时器为此锦上添花。 And I was wondering, if the same is made in SWT? 我想知道,是否在SWT中也是如此?

Probably there is no direct equivalent, so I made a class with util's Timer: 可能没有直接的等效项,因此我使用util的Timer创建了一个类:

public abstract class DelayedInfrequentAction implements Runnable {

    private int delay;
    private Display display;

    private Timer timer = null;

    public DelayedInfrequentAction(Display display, int delay) {
        this.display = display;
        this.delay = delay;
    }

    public synchronized void kick() {

        if( timer != null ) {
            timer.cancel();
            timer.purge();
            timer = null;
        }

        timer = new Timer(this.getClass().getSimpleName(), true);
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                display.syncExec(DelayedInfrequentAction.this);
                synchronized (DelayedInfrequentAction.this) {
                    timer = null;
                }

            }}, delay);

    }

    @Override
    abstract public void run();

}

this is not Swing's Timer equivalent, but utilizes cancelation ability of utils Timer. 这不是Swing的Timer等效项,而是利用utils Timer的取消功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM