ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Create Desktop Applications with Java-Based Web Technologies
Pages: 1, 2, 3, 4, 5, 6

In the interest of completeness, Example 3 describes the actual Swing user interface needed to build the application. I'll be honest: I knocked this off in a few minutes using NetBeans 3.5.

Example 3. ServerUI source

package onjava;

import java.awt.event.KeyEvent;
import java.awt.Toolkit;
import javax.swing.KeyStroke;

public class ServerUI extends javax.swing.JFrame
{
    
   /** Creates new form ServerUI */
   public ServerUI()
   {
      initComponents();
   }
    
   public int preferredMetaKey = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
    
   private void initComponents()
   {
      statusLabel = new javax.swing.JLabel();
      menuBar1 = new javax.swing.JMenuBar();
      fileMenu1 = new javax.swing.JMenu();
      browserMenuItem = new javax.swing.JMenuItem();
      fileMenuSep1 = new javax.swing.JSeparator();
      quitMenuItem = new javax.swing.JMenuItem();

      getContentPane().setLayout(null);

      setTitle("Canteen");
      addWindowListener(new java.awt.event.WindowAdapter()
      {
         public void windowClosing(java.awt.event.WindowEvent evt)
         {
            exitForm(evt);
         }
      });

      statusLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      statusLabel.setText("Launching...");
      getContentPane().add(statusLabel);

      fileMenu1.setText("File");
      browserMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, preferredMetaKey));
      browserMenuItem.setMnemonic('K');
      browserMenuItem.setText("Launch Browser");
      browserMenuItem.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            browserMenuItemActionPerformed(evt);
         }
      });

      fileMenu1.add(browserMenuItem);

      fileMenu1.add(fileMenuSep1);

      quitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, preferredMetaKey));
      quitMenuItem.setMnemonic('K');
      quitMenuItem.setText("Quit");
      quitMenuItem.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            quitMenuItemActionPerformed(evt);
         }
      });

      fileMenu1.add(quitMenuItem);

      menuBar1.add(fileMenu1);

      setJMenuBar(menuBar1);
        
      statusLabel.setLocation(5, 20);
      statusLabel.setSize(290, 25);
      this.setSize(300, 100);
   }
    
   private void browserMenuItemActionPerformed(java.awt.event.ActionEvent evt)
   {
      try
      {
         edu.stanford.ejalbert.BrowserLauncher.openURL("http://127.0.0.1:8080/"); }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }
    
   private void quitMenuItemActionPerformed(java.awt.event.ActionEvent evt)
   {
      exitForm(null);
   }
    
   /** Exit the Application */
   private void exitForm(java.awt.event.WindowEvent evt)
   {
      this.setStatus("Shutting down...");
      onjava.EmbeddedTomcat.stopServer();
      System.exit(0);
   }
    
   public void setStatus(String in)
   {
      statusLabel.setText(in);
        
   }
    
   public static void main(String args[])
   {
      new ServerUI().show();
   }
    
    
   private javax.swing.JMenuItem browserMenuItem;
   private javax.swing.JMenu fileMenu1;
   private javax.swing.JSeparator fileMenuSep1;
   private javax.swing.JMenuBar menuBar1;
   private javax.swing.JMenuItem quitMenuItem;
   private javax.swing.JLabel statusLabel;
   
}

As shown in Figures 1, 2, and 3, there's room for improvement, but this gets the job done. Purists will note that the menu bar is incorrect (again, information on how to solve that particular problem can be found in my book). You'll notice, however, that the example works on both Mac OS X and Windows XP.


Figure 1. Server UI on Mac OS X


Figure 2. Server UI menu on Mac OS X


Figure 3. Server UI on Windows XP

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow