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?

2
  • 1
    possible duplicate: Invalid Thread Access Error with Java SWT
    – jens-na
    Nov 21, 2012 at 7: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, 2013 at 9:54

2 Answers 2

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

2

You have do to this with asyncExec();:

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

Links:

2
  • Funny, we both pasted the same URL and its not linking correctly. Nov 21, 2012 at 6:56
  • 1
    fixed the link in my post: s/?/%3F/ :-)
    – jens-na
    Nov 21, 2012 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.