Implement a J2EE-aware application console in Swing
Use JMS to query and control your enterprise application from a Swing console
By John Chamberlain, JavaWorld.com, 11/30/01
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
An essential part of complex enterprise applications is a console. Consoles are the simplest but most flexible user interface.
They provide a window that allows the developer or system operator to type a command and receive a text response. Operating
systems invariably offer a system console; even the Mac has one, called the Macintosh Programmer's Workshop (MPW).
In an enterprise or application service provider (ASP) environment, the console provides a window into a system's operation
and allows operators to configure and control the system in real time. The console can also display unprompted status information
and announcements.
In this article, I'll show you how to construct a generic console from Swing components that uses the Java Messaging Service
(JMS) to interact with one or more application subsystems. JMS provides a standard solution to the problem of communication
between the backend system's command servers and their clients.
Figure 1 shows the console client's on-screen appearance. The user types commands in the interface's lower box (JTextField) and receives an answer in the text area above it. The dot before the command indicates that the command goes to the console
itself rather than to the connected host or queue. When text fills the text area, scroll bars automatically appear.

Figure 1. The console in action with the default Metal look and feel
Rather than create a distributed communication system in one step, we will build up to the finished product in stages. First,
we will construct a basic console that operates synchronously using a socket. This introduces the Swing fundamentals and demonstrates
how to build a functional console without the extra JMS complexities. Then we will extend our basic console with useful features,
such as tabbed panes. Finally, we will add JMS for industrial-strength communications.
Before getting started
The biggest hurdle to starting with Swing is understanding its underlying object model. Your application or applet must sit
above a complex web of classes with subtle and hidden interactions. Later in this article, I will cover some other important
topics, such as event threads and peers. To get started, you just need to know the component model. Mastering this foundation
takes time, but the inheritance outline below may give you a leg up on the learning curve:
Swing's inheritance model
java.awt.Component (abstract)
java.awt.Container
javax.swing.JComponent (abstract)
javax.swing.JRootPane
...other concrete components (JPanel, JTree etc.)
java.awt.Window
java.awt.Panel
java.applet.Applet
javax.swing.JApplet
javax.swing.JWindow
java.awt.Dialog
javax.swing.JDialog
java.awt.Frame
javax.swing.JFrame
The top-level Swing components (JApplet, JWindow, JDialog, and JFrame) are in bold above. Each of these containers has only one component, JRootPane. The diagram below illustrates the JRootPane -- the key to Swing.

Figure 2. The organization of Swing's JRootPane
The root pane has a glass pane on top and a layered pane underneath. The layered pane is composed of a MenuBar and a ContentPane. You add subcomponents to your outer container via the content pane. When you use a component's getContentPane() method, you are actually getting its root pane's content pane.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- Download the source code for this article
http://www.javaworld.com/javaworld/jw-11-2001/j2ee/j2eesource.zip
- For more articles by John Chamberlain, visit his Website
http://johnchamberlain.org
- Swing resources:
- "Fundamentals of JFC/Swing, Part 1" (March 1999) is a short course on Swing from the MageLang Institute, featured on Sun's
Developer Connection
http://developer.java.sun.com/developer/onlineTraining/GUI/Swing1/index.html
- The Java Class Librariesjava.applet, java.awt, java.beans (Volume 2), Patrick Chan and Rosanna Lee (Addison Wesley Longman, March 1998; ISBN0201310031)
http://www.amazon.com/exec/obidos/ASIN/0201310031/javaworld
- Graphic Java 1.2, Mastering the JFCAWT (Volume 1), David M. Geary (Prentice Hall, September 1998; ISBN 0130796662)
http://www.amazon.com/exec/obidos/ASIN/0130796662/javaworld
- Graphic Java 2, Swing (Volume 2), David M. Geary (Prentice Hall, March 1999; ISBN0130796670)
http://www.amazon.com/exec/obidos/ASIN/0130796670/javaworld
- Definitive Guide to Swing for Java 2, Second Edition, John Zukowski (APress, January 2000; ISBN189311578X)
http://www.amazon.com/exec/obidos/ASIN/189311578X/javaworld
- Java Swing, Robert Eckstein, Marc Loy, and Dave Wood (O'Reilly & Associates, September 1998; ISBN156592455X)
http://www.amazon.com/exec/obidos/ASIN/156592455X/javaworld
- The Model-View-Controller architecture is described in Design PatternsElements of Reusable Object-Oriented Software, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (Addison-Wesley, 1995; ISBN0201633612)
http://www.amazon.com/exec/obidos/ASIN/0201633612/javaworld
- JMS resources:
- Get the iPlanet Message Queue for Java 2.0 (formerly Sun's Java Message Queue)
http://iplanet.com/downloads/developer/2096.html
- Sun's JMS tutorial
http://java.sun.com/products/jms/tutorial/index.html
- Sun's message-driven bean trail
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/MDB.html
- Currently the best JMS reference is Professional JMS, Scott Grant et al. (Wrox Press, March 2001; ISBN1861004931)
http://www.amazon.com/exec/obidos/ASIN/1861004931/javaworld
- JavaWorld resources:
- Also in this week's Spotlight on Swing"Create a Scrollable Virtual Desktop in Swing," Tom Tessier (JavaWorld, November 2001)
http://www.javaworld.com/javaworld/jw-11-2001/jw-1130-jscroll.html
- For more JavaWorld articles on Swing, visit our AWT/Swing Index
http://www.javaworld.com/channel_content/jw-awt-index.shtml
- For more JavaWorld articles on JMS, visit our Java Message Service Index
http://www.javaworld.com/channel_content/jw-message-index.shtml
- For more JavaWorld articles on J2EE, visit our Java 2 Platform, Enterprise Edition Index
http://www.javaworld.com/channel_content/jw-j2ee-index.shtml
- Subscribe to JavaWorld's free weekly email newsletters
http://www.idg.net/jw-subscribe
- Speak out in JavaWorld's Java Forum
http://forums.idg.net/webx?13@@.ee6b802
- You'll find a wealth of IT-related articles from our sister publications at IDG.net