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

    Default SWT and Threads on Mac OS X


    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() {
    while (!display.isDisposed()) {
    if (!display.readAndDispatch()) {

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

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

    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(
    at org.eclipse.swt.SWT.error(
    at org.eclipse.swt.SWT.error(
    at org.eclipse.swt.widgets.Display.error( :1076)
    at org.eclipse.swt.widgets.Display.createDisplay(Disp
    at org.eclipse.swt.widgets.Display.create(Display.jav a:816)
    at<init>( :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(

    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 offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: SWT and Threads on Mac OS X

    at org.eclipse.swt.widgets.Display.<init>(Display.jav a:698)
    at view.View.initComponents(
    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
    Rep Power

    Default Re: SWT and Threads on Mac OS X


    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,


    With kind regards,


Similar Threads

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