SWT table update delay

We have a ViewerFilter for the TableViewer , which is a little slow, so to try to create an impression of awesomeness, we wanted the viewer to wait 500 milliseconds before updating the window (otherwise it would be blocked after each key press).

Having no idea what I was doing, I tried to create a class that would check if System.currentTimeMillis () was longer than the last time the + 500 key was pressed from another thread. It just threw an invalid stream access exception, so I got lost.

Edit: I was able to use TableViewer.getTable (). getDisplay (). asyncExec () to get around the problem with the wrong thread, but I do not like my solution, and I would really like to hear other suggestions.

+3
source share
2 answers

You might want to disable redrawing when updating the viewer.

Viewer.getControl().setRedraw(false);
// update
Viewer.getControl().setRedraw(true);

Sometimes this can give a better user interface. You can also schedule a ui task that you cancel when the user clicks on a new key or changes the text. For instance.

class RefreshJob extends WorkbenchJob
{
        public RefreshJob()
        {
            super("Refresh Job");   
            setSystem(true); // set to false to show progress to user
        }

        public IStatus runInUIThread(IProgressMonitor monitor)
        {
            monitor.beginTask("Refreshing", ProgressMonitor.UNKNOWN);
            m_viewer.refresh();
            monitor.done(); 
            return Status.OK_STATUS;
        };
}

and then reschedule the update in a separate task.

private RefreshJob              m_refreshJob = new RefreshJob();   
private Text                    m_filterText;
void hookModifyListener()
{
    m_filterText.addModifyListener(new ModifyListener()
    {
        public void modifyText(ModifyEvent e)
        {
            m_refreshJob.cancel();
            m_refreshJob.schedule(500);
        }
    });
}

If the user presses the Enter key, you can schedule the update task without delay,

+2
source

display.syncExec, :

Display.getDefault().asyncExec(new Runnable() {
 public void run() {
      // check refresh time
      // refresh.
 }
});

, asyncExec, syncExec .

0

Source: https://habr.com/ru/post/1729963/


All Articles