Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Implement a J2EE-aware application console in Swing

Use JMS to query and control your enterprise application from a Swing console

  • 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
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources