Results 1 to 3 of 3
  1. #1
    lira is offline Member
    Join Date
    Apr 2014
    Posts
    1
    Rep Power
    0

    Default SWT and Threads on Mac OS X

    Hello,

    I'm new to Java and trying to write code a Java program on Mac OS X using IntelliJ. My program uses the SWT library and contains two class's; the first called "view" and the second called "main".

    The "view" class defines the SWT objects, extends the "Thread" class and contains a "run" method;

    public void run() {
    initComponents();
    while (!display.isDisposed()) {
    if (!display.readAndDispatch()) {
    display.sleep();
    }
    }
    display.dispose();
    }


    The "main" class contains the main method as following:

    public static void main(String[] args) {
    View view = new View();
    view.start();
    }


    I'm getting those error messages when I'm trying to run this program:

    ***WARNING: Display must be created on main thread due to Cocoa restrictions.
    Exception in thread "Thread-0" org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(SWT.java:4397)
    at org.eclipse.swt.SWT.error(SWT.java:4312)
    at org.eclipse.swt.SWT.error(SWT.java:4283)
    at org.eclipse.swt.widgets.Display.error(Display.java :1076)
    at org.eclipse.swt.widgets.Display.createDisplay(Disp lay.java:833)
    at org.eclipse.swt.widgets.Display.create(Display.jav a:816)
    at org.eclipse.swt.graphics.Device.<init>(Device.java :130)
    at org.eclipse.swt.widgets.Display.<init>(Display.jav a:707)
    at org.eclipse.swt.widgets.Display.<init>(Display.jav a:698)
    at view.View.initComponents(View.java:22)
    at view.View.run(View.java:87)

    I searched for a solution and saw that I have to use the "-XstartOnFirstThread" parameter to JVM. I'm trying it with no success.

    Please advise me about this issue.

    Thank you.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,446
    Rep Power
    25

    Default Re: SWT and Threads on Mac OS X

    at org.eclipse.swt.widgets.Display.<init>(Display.jav a:698)
    at view.View.initComponents(View.java:22)
    at view.View.run(View.java:87)
    Looks like the problem was at line 22 in initComponents where it is calling the Display constructor.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: SWT and Threads on Mac OS X

    Hi,

    Is there a specific reason why you want to have any SWT items in some other thread?

    The normal pattern is, that your main thread is also the UI thread and is handling all GUI stuff. And that thread is only managing the UI - so you do nothing that required more time. So if you have things that take some time, then this is part of the business layer (which I always keep separate from the GUI. That way it is easier to unit test and it is much easier to build new frontends). And if you have some other thread that needs to update the GUI, then you have to do that inside the UI Thread. All UI Components that I know have a standard way to do it (which often simply results in a message to the application - the same stuff that is happening with all other events). The SWT way for this is the asyncExec call if I understood SWT correctly. (I do not have much experience with SWT. I just read a little about the core concepts before deciding that I do not want to use it.)

    Something else that I saw:
    Your while look runs till display is disposed - and then you dispose the display? Seems strange. As soon as the display is disposed, you do not have to dispose it again. And if your while loop is working with the display, then you could run into trouble when the display is disposed and you still try to work with it.

    I hope that I was of a little help. I am not a SWT expert but I have the hope that it was useful for you.

    With kind regards,

    Konrad

    With kind regards,

    Konrad

Similar Threads

  1. threads
    By kyle_maddisson in forum New To Java
    Replies: 1
    Last Post: 12-10-2011, 03:12 AM
  2. New to threads
    By armyson in forum Threads and Synchronization
    Replies: 0
    Last Post: 12-09-2011, 07:52 AM
  3. Threads per Connection or Threads per Request
    By Felic in forum New To Java
    Replies: 4
    Last Post: 11-22-2011, 09:15 PM
  4. Threads
    By GoLuM83 in forum Advanced Java
    Replies: 7
    Last Post: 03-31-2010, 08:52 PM
  5. Threads!
    By rameshraj in forum Advanced Java
    Replies: 1
    Last Post: 05-04-2008, 04:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •