1

I have a JFaceTableViewer with many rows. These rows I have previously fetched by using JPA. I have put the fetching in another thread to show a progress while fetching and it works.

Now I have put an SelectionAdapter on each of the columns to allow sorting on that column. Here is the code:

private SelectionAdapter getSelectionAdapter(final TableColumn column, final String colName) {
    SelectionAdapter selectionAdapter = new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            comparator.setColumnName(colName);
            int dir = comparator.getDirection();
            resultTableViewer.getTable().setSortDirection(dir);
            resultTableViewer.getTable().setSortColumn(column);
            resultTableViewer.refresh();                
          }
        };
        return selectionAdapter;
      }

How can I execute these lines for sorting in another thread to show a progressbar? Everytime I try to call methods on resultTableViewer I got the exception: InvalidThreadAccess

Any ideas?

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • 1
    possible duplicate: [Invalid Thread Access Error with Java SWT](http://stackoverflow.com/questions/5980316/invalid-thread-access-error-with-java-swt) – jens-na Nov 21 '12 at 07:03
  • @WildDogSmith: Welcome to StackOverflow. People have tried to answer your question. If one of them solves your problem, you should let the author know by selecting the check-mark next to the answer. – oberlies Aug 13 '13 at 09:54

2 Answers2

2

You are getting invalid thread access because you are not executing your code above in the UI thread which is a requirement for updating SWT widgets. What you can do is something like this, keep you existing method structure but have it execute in the UI thread that follows this pattern where you get the Display from the table widget and you will not get the invalid thread access exception.

    text.getDisplay().asyncExec(new Runnable() {

        @Override
        public void run() {
            StringWriter writer = new StringWriter();
            for (String string : content) {
                writer.append(string + "\n");

            }
            text.setText(writer.toString());
        }
    });

Look here for more information about your issue: Invalid SWT Thread Access

Duncan Krebs
  • 3,366
  • 2
  • 33
  • 53
2

You have do to this with asyncExec();:

    Display.getDefault().asyncExec(new Runnable() {
       public void run() {
          // your sorting here
       }

Links:

jens-na
  • 2,254
  • 1
  • 17
  • 22