简体   繁体   中英

How can we change the label after a specific time for a composite in swt

I have created a label in a composite and now i need to change the label after every 60sec so how can we do that.The code sample for creating the label is as follows.

Label status = new Label(rightCompositeStatusbar, SWT.NONE);
status.setText("save successful");

so now after 60secs the label name has to be changed. Please help me to do.

Use Display#timerExec(int, Runnable) :

Causes the run() method of the runnable to be invoked by the user-interface thread after the specified number of milliseconds have elapsed. If milliseconds is less than zero, the runnable is not executed.

Note that at the time the runnable is invoked, widgets that have the receiver as their display may have been disposed. Therefore, it is necessary to check for this case inside the runnable before accessing the widget.

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

    final Label status = new Label(shell, SWT.NONE);
    status.setText("0");

    display.timerExec(100, new Runnable()
    {
        int i = 1;

        @Override
        public void run()
        {
            if(!status.isDisposed())
                status.setText(i++ + "");

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

    shell.pack();
    shell.open();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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