Results 1 to 2 of 2
  1. #1
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default Invalid Thread Access?

    I have a Composite shell that has a button set-up to open the file dialog within windows but whenever I put the commands into the selection listener I get an invalid thread access error when I try to open that view.
    Java Code:
    //file path
    	    new Label(banner, SWT.NONE).setText("Build File: ");
    	    fdText = new Text(banner, SWT.BORDER);	    
    	    Button open = new Button(banner, SWT.PUSH);
    	    open.setText("Open");
    		gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
    	    open.setLayoutData(gridData);
    	    	    
    	    shell = new Shell(new Display());
    	    shell.setSize(400, 400);
    	    open.addSelectionListener(new SelectionAdapter() {
    	    	public void widgetSelected(SelectionEvent event){
    	    		FileDialog fd = new FileDialog(shell, SWT.OPEN);
    	    	    fd.setText("Open");
    	    	    fd.setFilterPath("C:/");
    	    	    String selected = fd.open();
    	    	    System.out.println(selected);      
    	    	}
    	    });
    if I remove the contents of widgetSelected() it will load the view but the button does nothing when its pressed. I was looking around and it seems I need to synchronize the FileDialog to the Windows action timer so that multiple commands don't try to run at the same time or something but I can't figure out how. The simplest example I've seen is
    Java Code:
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.widgets.FileDialog;
    import org.eclipse.swt.widgets.Shell;
    
    public class MainClass {
    
      public static void main(String[] a) {
        Shell s = new Shell();
        
        FileDialog fd = new FileDialog(s, SWT.OPEN);
        fd.setText("Open");
        fd.setFilterPath("C:/");
        String[] filterExt = {"*.txt","*.doc", ".rtf", "*.*"};
        fd.setFilterExtensions(filterExt);
        String selected = fd.open();
        System.out.println(selected);                    
      }
    }
    The only thing I can think is that I need place code thats in widgetSelected to open from Main but I don't seem to have a main method.(Running Eclipse Ganyemede). Anyone know how to fix this so I can have a button within view activate the FileDialog in a new shell?

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    I looked up Threads a little and added this to my code. It now runs the view instead of just displaying an error message. If I click on the open button nothing happens until I close the application and then an error message appears.
    Java Code:
     //file path
    	    new Label(banner, SWT.NONE).setText("Build File: ");
    	    fdText = new Text(banner, SWT.BORDER);	    
    	    Button open = new Button(banner, SWT.PUSH);
    	    open.setText("Open");
    		gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
    	    open.setLayoutData(gridData);
    	    	    
    	    open.addSelectionListener(new SelectionAdapter() {
    	    	public void widgetSelected(SelectionEvent event){
    	    		 System.out.println("Testing before open");
    	    		 
    	    		 new Thread(new Runnable() {
    	    		      public void run() {
    	    		         while (true) {
    	    		            try { Thread.sleep(1000); } catch (Exception e) { }
    	    		            Display.getDefault().asyncExec(new Runnable() {
    	    		               public void run() {
    	    		            	   Display d = new Display();
    	    		  	    		   Shell s = new Shell(d);
    	    		  	    		   System.out.println("Testing Thread");
    	    		  	    		   FileDialog fd = new FileDialog(s, SWT.OPEN);
    	    		  		           fd.setText("Open");
    	    		  		           String selected = fd.open();
    	    		  		           System.out.println(selected);
    	    		  		       }
    	    		            });
    	    		         }
    	    		      }
    	    		   }).start();
    		         System.out.println("Testing after open");
    	    	}
    	    });
    This is the error
    Java Code:
    !ENTRY org.eclipse.ui 4 0 2008-10-30 15:43:23.924
    !MESSAGE Unhandled event loop exception
    !STACK 0
    org.eclipse.swt.SWTException: Invalid thread access
    	at org.eclipse.swt.SWT.error(SWT.java:3777)
    	at org.eclipse.swt.SWT.error(SWT.java:3695)
    	at org.eclipse.swt.SWT.error(SWT.java:3666)
    	at org.eclipse.swt.widgets.Display.checkDisplay(Display.java:714)
    	at org.eclipse.swt.widgets.Display.create(Display.java:776)
    	at org.eclipse.swt.graphics.Device.<init>(Device.java:135)
    	at org.eclipse.swt.widgets.Display.<init>(Display.java:447)
    	at org.eclipse.swt.widgets.Display.<init>(Display.java:438)
    	at views.NewBuildView$1.widgetSelected(NewBuildView.java:84)
    	at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:228)
    	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1003)
    	at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3823)
    	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3422)
    	at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2382)
    	at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2346)
    	at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2198)
    	at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:493)
    	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:288)
    	at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:488)
    	at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    	at novlayout.Application.start(Application.java:20)
    	at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:193)
    	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:386)
    	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:549)
    	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:504)
    	at org.eclipse.equinox.launcher.Main.run(Main.java:1236)
    	at org.eclipse.equinox.launcher.Main.main(Main.java:1212)
    The console output right now is just Testing before open Testing after open.

Similar Threads

  1. Replies: 2
    Last Post: 08-14-2009, 10:16 AM
  2. Replies: 0
    Last Post: 01-28-2008, 07:02 AM
  3. Replies: 1
    Last Post: 08-07-2007, 07:51 AM
  4. how to access to an object from a thread
    By tamayo in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 04:24 AM

Posting Permissions

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