-1

I have a bit of a problem. I'm trying to compare a string to an array. The array is built up after receiving strings on a serial line.

I use several voids and pass the strings and arrays between them so every thing must be static.

// button is set
public static  Button btnRS_1;
// inside Create contense:
            btnRS_1 = new Button(shlSioMiniTest, SWT.TOGGLE);
        btnRS_1.setEnabled(false);
        btnRS_1.setSelection(false);;
        btnRS_1.setFont(SWTResourceManager.getFont("Tahoma", 10, SWT.NORMAL));
        btnRS_1.setBounds(29, 130, 76, 16);
        btnRS_1.setText("RS232 0");

        if (RStest[0].equals("1")){
          btnRS_1.setSelection(true);
           }

I get errors. A lot of them too. And if I comment out the setSelection bit and just print out a string to see it works.

Exception in thread "Thread-9" org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(SWT.java:4361)
    at org.eclipse.swt.SWT.error(SWT.java:4276)
    at org.eclipse.swt.SWT.error(SWT.java:4247)
    at org.eclipse.swt.widgets.Widget.error(Widget.java:468)
    at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:359)
    at org.eclipse.swt.widgets.Button.setSelection(Button.java:1047)
    at SioTest.Sio_test.fillChecks(Sio_test.java:604)
    at SioTest.Sio_test.testParameters(Sio_test.java:592)
    at SioTest.Sio_test$ProtocolImpl.onMessage(Sio_test.java:968)
    at SioTest.Sio_test$ProtocolImpl.onReceive(Sio_test.java:946)
    at SioTest.Sio_test$SerialReader.run(Sio_test.java:882)
    at java.lang.Thread.run(Unknown Source)

Can someone advice me on what to do?

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
KarlNorway
  • 55
  • 1
  • 1
  • 8
  • 2
    Please read the [SWT FAQ](http://wiki.eclipse.org/FAQ_Why_do_I_get_an_invalid_thread_access_exception%3F). The answer to your question can be found there. – Baz Feb 26 '13 at 12:20
  • Or [this](http://stackoverflow.com/questions/5980316/invalid-thread-access-error-with-java-swt) other question... – Baz Feb 26 '13 at 12:21
  • I have seen the other thread ( Invalid Thread Access Error with Java SWT ) here but cant say that it helped me much. – KarlNorway Feb 26 '13 at 12:49
  • Just wrap the `setSelection` call in the `Display.getDefault().asyncExec` thingy you can see in the answer... You have to wrap every change to the GUI in these calls if they happen from outside the main thread... – Baz Feb 26 '13 at 12:50

1 Answers1

-1

Have a read at the following link : Invalid access exception

Looks like your calling something from outside of the SWT Display thread

Michael W
  • 3,515
  • 8
  • 39
  • 62
  • -1 for trying to help someone. And then no comment to say why???? Thank you very much :) – Michael W Feb 26 '13 at 12:29
  • 1
    I agree, a -1 is not necessary, although I already posted two links to other solutions in the comments. You should mark this question as a duplicate instead of posting a duplicate answer. – Baz Feb 26 '13 at 12:38
  • Thanks Mick but I cant say I can figure it out by looking and reading the FAQ. – KarlNorway Feb 26 '13 at 12:46
  • @KarlNorway Look at the answer to the second link I posted to your question. – Baz Feb 26 '13 at 12:46