SWT Newbie question ... Opening forms/windows

SWT Designer allows you to create the views, editors, perspectives, pref pages, composites, etc. that comprise Eclipse SWT & RCP applications and plug-ins.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

SWT Newbie question ... Opening forms/windows

Postby Hansz » Fri Jan 16, 2004 1:35 am

I've created a few windows, using swt-designer, and now I want to do open a simple 'about' window from the main menu in the main form. Simple .. that's what I thought. But it won't work .. I constantly run into this exception: org.eclipse.swt.SWTException: Invalid thread access

I've been trying to find some code snippet or example on the web that explains how I can 'navigate' the windows I want to put in my application. Browsing the swt newsgroups also didn't help.

I must be missing something

Here's the piced of code I'm calling form the widgetSelected event handler

display.asyncExec(
new Runnable () {
public void run() {
AboutForm about = new AboutForm();
about.open();
}
}
);


I've tried with and without the runnable part (with syncExec and asyncExec)

Any help would be greatly appreciated ...
Thnx in advance for your time and efforts.
Even the smallest person can change the course of the future - Galadriel
Hansz
 
Posts: 18
Joined: Thu Jan 15, 2004 12:45 am
Location: Hoofddorp, Netherlands

Re: SWT Newbie question ... Opening forms/windows

Postby Eric Clayberg » Fri Jan 16, 2004 8:50 pm

Hansz wrote:I've created a few windows, using swt-designer, and now I want to do open a simple 'about' window from the main menu in the main form. Simple .. that's what I thought. But it won't work .. I constantly run into this exception: org.eclipse.swt.SWTException: Invalid thread access

There is nothing particularly difficult about doing this. You should be able to open one window from another very easily. The worst thing you would need to do (in the event that you are somehow trying to launch the window from some thread other than the UI thread) is open the window inside a syncExec() or asyncExec() runnable. If that is not working, you should post a complete, self-contained example of what you are doing. Without seeing that, it would be hard to speculate what the problem might be.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

I am having the same problem

Postby swtbegger » Tue Apr 13, 2004 10:15 pm

I am trying to open another form I have created (GUITest). here is the code that bombs on gt.open();
Code: Select all
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

/*
* Created on Apr 14, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class GUItest2 {
   public static void main(String[] args) {
      GUItest2 window = new GUItest2();
      window.open();
   }
   public void open() {
      final Display display = new Display();
      final Shell shell = new Shell();
      shell.setLayout(new FormLayout());
      shell.setText("SWT Application");
      {
         final Button button = new Button(shell, SWT.NONE);
         button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
               GUITest gt = new GUITest();
               gt.open();                  
            }
         });
         final FormData formData = new FormData();
         formData.bottom = new FormAttachment(0, 115);
         formData.right = new FormAttachment(0, 135);
         formData.top = new FormAttachment(0, 80);
         formData.left = new FormAttachment(0, 75);
         button.setLayoutData(formData);
         button.setText("button");
      }
      {
         final Text text = new Text(shell, SWT.BORDER);
         final FormData formData = new FormData();
         formData.bottom = new FormAttachment(0, 155);
         formData.right = new FormAttachment(0, 295);
         formData.top = new FormAttachment(0, 125);
         formData.left = new FormAttachment(0, 205);
         text.setLayoutData(formData);
      }
      shell.open();
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch())
            display.sleep();
      }
   }
}

when I click the button, the JVM bombs and i get this error:
Code: Select all
org.eclipse.swt.SWTException: Invalid thread access
   at org.eclipse.swt.SWT.error(SWT.java:2625)
   at org.eclipse.swt.SWT.error(SWT.java:2555)
   at org.eclipse.swt.widgets.Display.checkDisplay(Display.java:533)
   at org.eclipse.swt.widgets.Display.create(Display.java:594)
   at org.eclipse.swt.graphics.Device.<init>(Device.java:98)
   at org.eclipse.swt.widgets.Display.<init>(Display.java:337)
   at org.eclipse.swt.widgets.Display.<init>(Display.java:333)
   at GUITest.open(GUITest.java:31)
   at GUItest2$1.widgetSelected(GUItest2.java:41)
   at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:89)
   at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:82)
   at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:769)
   at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:2578)
   at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2256)
   at GUItest2.open(GUItest2.java:63)
   at GUItest2.main(GUItest2.java:29)
Exception in thread "main"

All I want to do is to be able to open a new form. Any tips?
swtbegger
 
Posts: 2
Joined: Tue Apr 13, 2004 10:09 pm

Postby raphael » Wed Apr 14, 2004 7:23 am

If you created another shell using the Designer, you should get the following:
Code: Select all
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class GUITest {
   public static void main(String[] args) {
      GUITest window = new GUITest();
      window.open();
   }
   public void open() {
      final Display display = new Display();
      final Shell shell = new Shell();
      shell.setText("SWT Application");
      shell.open();
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch())
            display.sleep();
      }
   }
}


You should only have one Display though. Here you have a display for each window. I tried just removing the Display declaration and the event loop in the GUITest class and it seemed to work. This is what I had afterwards.
Code: Select all
import org.eclipse.swt.widgets.Shell;

public class GUITest {
   public static void main(String[] args) {
      GUITest window = new GUITest();
      window.open();
   }
   public void open() {
      final Shell shell = new Shell();
      shell.setText("SWT Application");
      shell.open();
   }
}


Your GUItest2 class is fine as it is.
raphael
 
Posts: 22
Joined: Thu Jan 29, 2004 3:41 pm

Postby swtbegger » Wed Apr 14, 2004 7:52 am

that fixed my problem. thank you very much!
swtbegger
 
Posts: 2
Joined: Tue Apr 13, 2004 10:09 pm


Return to SWT Designer

Who is online

Users browsing this forum: No registered users and 1 guest