[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.rcp] Re: Problem using Swing from org.eclipse.core.runtime.applications extension

Thank you for this information: as you pointed out the problem was that I
misunderstood how the setVisible method works. It blocks for a modal
JDialog but not for a JFrame. The following IPlatformRunnable does the
right thing:

package clo.brp.test.eclipse.rcp.test1;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

import org.eclipse.core.runtime.IPlatformRunnable;

public class Application implements IPlatformRunnable {

    public Object run(Object args) {
    	
        MainFrame frame = new MainFrame();
        frame.setVisible(true);
        frame.waitForClosure();
    	
        System.out.println("frame closed");
    	
        return IPlatformRunnable.EXIT_OK;
        
    }
    
    private class MainFrame extends JFrame {
    	
        private Object mSyncObject = new Object();
    	
        public MainFrame() {
    		
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        	
            addWindowListener(new WindowAdapter() {
                public void windowClosed(WindowEvent inEvent) {
                    synchronized (mSyncObject) {
                        mSyncObject.notifyAll();
                    }
                }
            });
        	
        }
    	
        public void waitForClosure() {
            synchronized (mSyncObject) {
                try {
                    mSyncObject.wait();
                } catch (InterruptedException e) { }
            }
        }
        
    }

}

Rafael Chaves wrote:

> What happens in a regular Java application is that Swing will create an
> additional thread. The call to setVisible(true) causes the thread to be
> created, but never blocks. The VM stays running the Swing thread, while
> the main thread runs the main method to completion and ends.

> The difference is that in Eclipse, when the application finishes running,
> Eclipse will start the shutdown sequence. And the last step in the
> shutdown sequence is to call System.exit(), what will exit the VM no
> matter how many threads are currently running.

> Your application should block waiting (using a monitor, for instance)
> until you believe it is time to exit (the windowClosing event, for
> instance). In the callback for that event, you could notify the monitor so
> the main thread would complete running, and Eclipse's shutdown sequence
> could start.

> Rafael

> Harold Mills wrote:

> > I have an existing Swing application to which I would like to add plug-in
> > support. I hope to use the Eclipse plug-in facility for this purpose. To
> > convince myself that this can work, I'm trying to write a small Eclipse
> > plug-in containing an org.eclipse.core.runtime.applications extension as
> > follows:

> > package clo.brp.rcp;

> > import javax.swing.JFrame;

> > import org.eclipse.core.runtime.IPlatformRunnable;

> > public class RcpApplication implements IPlatformRunnable {

> >     public Object run(Object args) {

> >     	JFrame frame = new JFrame("RcpApplication");
> >     	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> >     	frame.setVisible(true);

> >     	System.out.println("back from setVisible");

> >     	return IPlatformRunnable.EXIT_OK;

> >     }

> > }

> > This almost works, except that the call to frame.setVisible returns
> > immediately instead of blocking until the user dismisses the JFrame, the
> > normal behavior for a Swing application. So the JFrame appears only
> > briefly and then the program exits. Does anybody know how I can fix this?