A first frame

The project is there, now it’s time to fill it with life. What is the task? First I want to display a JFrame when the bundle gets activated. I think this is typical for a client application that there is at least a main frame for interaction with the user. Also from a lifecycle view the application must exit when the user closes the frame. So I started creating the implementation of BundleActivator for my application bundle with the lifecycle methods.

Creating a frame in the #start(BundleContext) method is fairly easy. But when writing Swing programs I have to consider what has to run on the event-dispatch thread (EDT) and what can run in a background thread. The creation of the frame must run in the EDT. So as mentioned in a previous post I use SwingUtilities#invokeAndWait(Runnable) method create and show the frame. I don’t use #invokeLater(Runnable) because in my case I want the frame to be shown during application startup. The #stop(BundleContext) method is the lifecycle counterpart to start. In this method I hide and dispose the frame again using SwingUtilities#invokeAndWait(Runnable). With this approach I force clients to move starting and stopping of bundles to a background thread because these operations can take seriously long and if they are done in the EDT an exception will be thrown from SwingUtilities.

public void start(final BundleContext context) throws Exception {
  if (frame == null) {
    // Startup the application frame using SwingUtilities to operate in the
    // EDT because this method has to be invoked in a different thread.
    SwingUtilities.invokeAndWait(new Runnable() {
      public void run() {
        frame = new ApplicationFrame(context);

The frame processes the window events and when the closing event is triggered it stops the system bundle in a background thread.

protected void processWindowEvent(WindowEvent e) {
  if (e.getID() == WindowEvent.WINDOW_CLOSING) {
    // TODO need to wrap this with SwingWorkerExt,
    // check if user rejects closing
    new Thread(new Runnable() {
      public void run() {
        try {
          // stopping bundle 0 (i.e. system bundle) shuts down the platform
          Bundle systemBundle = context.getBundle(0);
        } catch (BundleException ex) {
        } finally {
          context = null;

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:


Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )


Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )


Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s