Using Swing in an Eclipse RCP application

I’ve finally made it to JavaOne. It’s my first time here and I have to admit that I’m a little overwhelmed. It’s a huge conference.

Today, the title of my blog is not an insane attempt to attract more traffic… I was working the Eclipse Foundation booth earlier today and was asked by a visitor about the possibility of building an Eclipse RCP application with a Swing-based user interface.

Why the heck would you want to do such a thing I can hear you ask? (actually, I think that might be somebody from the Oracle booth) There are several reasons. My favourite reason is Equinox. You can use the Equinox component framework under your Swing application. And if you do that, you can make use of things like extension points in your application. This, of course, makes your application very extendable. Of course, by using Equinox, you can also leverage the Eclipse update mechanism. Then, there’s help: you can probably use a little help with your Swing application. For me the biggest reason for using Equinox is simple: actual managed components.

By making your Swing-based user interface an Eclipse RCP application, you can make use of Eclipse plug-ins. Of course, if those plug-ins require SWT or the workbench to run (and you don’t want those things to be part of your application), you’re out of luck. But components packaged up as OSGi bundles can be used. So you could use the Hibernate bundles or package up other libraries that you might need as bundles to make them more easily updated later.

Running a Swing-based GUI from Eclipse RCP is pretty easy. When you generate an RCP applciation, it creates code like this:

public Object run(Object args) throws Exception {
Display display = PlatformUI.createDisplay();
try {
int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
if (returnCode == PlatformUI.RETURN_RESTART) {
return IPlatformRunnable.EXIT_RESTART;
}
return IPlatformRunnable.EXIT_OK;
} finally {
display.dispose();
}
}

The createAndRunWorkbench(…) part is where (oddly enough) the workbench gets created and run (I do love descriptive names).

You can change it to instead do Swing stuff:

public Object run(Object args) throws Exception {
JFrame frame = new JFrame("Eclipse RCP");
frame.setLocation(50, 50);
frame.add(new JTextField("Eclipse RCP application with Swing!"));
frame.pack();
frame.setVisible(true);
while (frame.isVisible())
Thread.currentThread().sleep(1000);
return IPlatformRunnable.EXIT_OK;
}

So… if you have an existing Swing application and you want to take advantage of the great features provided by the Eclipse RCP, you might consider this as a first step in your migration.

This entry was posted in Uncategorized. Bookmark the permalink.

5 Responses to Using Swing in an Eclipse RCP application

  1. zx says:

    Phillip’s last few posts have gotten me antsy… so I’ll say it… lipstick on a pig ;p

  2. Jeroen says:

    Over here we’re actually using a Graphics2D based drawing toolkit to create diagrams within an RCP application.If that isn’t disturbing. 😉

  3. beche-de-mer says:

    Wayne,I am actually (nearly done) developing a Swing application that I think would a great fit for Equinox. It’s modular by design but monolithic for distribution. A couple of “Manager” classes store hand-coded arrays of class names for the various components and can instantiate them on demand. Each component is developed as a seperate Eclipse java project and packaged individually as its own JAR. I would love to have the Manager classes use an extension registry and update all of the components to be OSGi bundles so that the Manager’s could dynamically determine which components are available at runtime and OSGi could take care of all the classpath issues. Distributing the components via an Eclipse-like update site would also be fantastic. I suspect I would make an extension point for Perspectives, Reports, and Schedules in my particular application.I have explored the Eclipse RCP a little but I ran into two problems. I could not figure out how to use an extension registry with just Equinox and trying to include some of the update plug-ins always required pulling in more plug-ins including SWT ones.Would you be able to post an example of creating an extesion point, defining an extension point schema, having some class that can reference or cache values from an extension point registry using just equinox, and a simple component that implementats that extension point?Also, I suspect that this will require Eclipse 3.2?Then an example of using the Eclipse update mechanism would be fantastic. For example, I think you need to use Features in order to use the update mechanisms but can that be done using just Equinox?One of the reasons Swing is useful in this context is that OSGi solves problems that are independent of graphical display and Swing has uses of its own. For instance, (a) I already started the program using it, (b) there are other libraries like JFreeChart and SwingX and data-binding libraries that are Swing-centric and (c) Swing documentation is much better than SWT documentation.

  4. Roland says:

    Wayne,currently I am working on this idea, too and I will talk about it on the W-JAX this year. Maybe I may improve these two lines of code:while (frame.isVisible()) Thread.currentThread().sleep(1000);This code has two disadvantages:1st: it polls every second, if the frame is visible and so needs some CPU time (even if not much)2nd (and more important): it needs up to one second to recognize that the user has exited the appSo it would be better if the app waits passively for a notify. This could happen like this:// Initialization Code/* First set the default closing behaviour from hiding to disposing */frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);frame.addWindowListener(new WindowAdapter() { public void windowClosed(WindowEvent e) { notifyFrameClosed(); }});// Some other codeframe.setVisible(true);waitForFrame(frame);return IPlatformRunnable.EXIT_OK;The two methods notifyFrameClosed() and waitForFrame(Frame) would look like this:private synchronized void notifyFrameClosed() { notifyAll();}private synchronized void waitForFrame(JFrame frame) throws InterruptedException { while (frame.isVisible()) { wait(); }}I hope I don’t tell anything that you already know. ;-)See you,RolandPS: Sorry for not formatting correctly, I didn’t know which HTML Tag to use

  5. Deep Shah says:

    hi Wayne,
    Thanks for the above code. But there are still problem which is a major problem for the application is that when i run above code it run JFrame successfully but i also apply splash screen in my application. So the problem is that when i launch the product and run it at that time splash scree appears first then JFrame also appears,but splash screen does not close automatically which is happen in default behavior of Eclipse RCP. So,what is the solution for that?

Leave a comment