2010/12/15 - Apache Excalibur has been retired.

For more information, please explore the Attic.

Swing Based Applications

Swing applications are those applications that have a lovely graphical interface for you to interact with. When you are done with the application, you close it and are done with it. Some examples of a Swing based application include Apache JMeter, JEdit, and Avalon Instrument Client.

Swing applications are very similar to CLI based applications in the way they are set up and torn down. What differs is that the GUI runs in a different set of threads, so the main routine has to wait until the GUI is done. There are two strategies to do that. The first is to extend the DefaultContainer and use that to enclose your Swing based JFrame and implement the Runnable interface. The second is to manage the Swing GUI separately, but give your container or its ServiceManager to the Swing code to interact with.

Extending DefaultContainer

If we extend the DefaultContainer, our main method will look pretty much the same:

                    
public int main(String [] args)
{
    // You would have to implement the referenced method here...
    FortressConfig config = configWithArgs( args );
    ContainerManager cm = new DefaultContainerManager( config.getContext() );
    ContainerUtil.initialize( cm );

    // Get the root container and use it
    ((MySwingContainer) cm.getContainer()).run();

    // Clean up after ourselves
    ContainerUtil.dispose( cm );
}
                  
                

The extensions to DefaultContainer that you do are different for each application. However, they will have some common elements. Here is an excerpt from both the Swing example and the Viewer example included in CVS.

                    
public class MySwingContainer extends DefaultContainer implements Runnable
{
     JFrame m_frame = new JFrame();

     // skipping non relevant parts

     public void run()
     {
         while ( m_frame.isVisible() )
         {
             try
             {
                 Thread.sleep(1000);
             }
             catch (InterruptedException ie)
             {
                 // you can either ignore the exception or you can
                 // close the application.  Remove the next line
                 // if you don't want to end the application:

                 m_frame.setVisible( false );
             }
         }
     }
}