t e m p o r a l 
 d o o r w a y 

The Simplest JBuilder Program

 

This program is a simple division calculator that shows how to make a reactive application. This calculator simply ignores invalid input by using exception handling in the shared Calculate() function.

Application

/**
* Title:            Basic Application<p>
* Description:   Reproduces the similar C++ Builder example allowing "calculator-like" functions, demonstrating exception handling, input and output.<p>
* Copyright:      Copyright (c) Mark Cashman<p>
* Company:         <p>
* @author Mark Cashman
* @version 1.0
*/
package basicapplication;

import javax.swing.UIManager;

public class MainApplication
{
   boolean packFrame = false;

   //Construct the application
   public MainApplication()
   {
      MainFrame frame = new MainFrame();
      //Validate frames that have preset sizes
      //Pack frames that have useful preferred size info, e.g. from their layout
      if (packFrame)
      {
         frame.pack();
      }
      else
      {
         frame.validate();
      }
      frame.setVisible(true);
   }

   //Main method
   public static void main(String[] args)
   {
      try
      {
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
      new MainApplication();
   }
}

Frame

/**
* Title:            Basic Application<p>
* Description:   Reproduces the similar C++ Builder example allowing "calculator-like" functions, demonstrating exception handling, input and output.<p>
* Copyright:      Copyright (c) Mark Cashman<p>
* Company:         <p>
* @author Mark Cashman
* @version 1.0
*/
package basicapplication;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.beans.*;
import javax.swing.event.*;

public class MainFrame extends JFrame
{
   JPanel contentPane;
   VerticalFlowLayout contentVerticalFlowLayout = new VerticalFlowLayout();
   Box outputBox;
   JLabel outputLabel = new JLabel();
   JLabel outputValue = new JLabel();
   JTextField input2Field = new JTextField();
   Box input2Box;
   JLabel input2Label = new JLabel();
   JTextField input1Field = new JTextField();
   Box input1Box;
   JLabel input1Label = new JLabel();

   //Construct the frame
   public MainFrame()
   {
      enableEvents(AWTEvent.WINDOW_EVENT_MASK);
      try
      {
         jbInit();
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
   }

   //Component initialization
   private void jbInit() throws Exception
   {
      contentPane = (JPanel) this.getContentPane();
      outputBox = Box.createHorizontalBox();
      input2Box = Box.createHorizontalBox();
      input1Box = Box.createHorizontalBox();
      contentPane.setLayout(contentVerticalFlowLayout);
      this.setResizable(false);
      this.setSize(new Dimension(400, 300));
      this.setTitle("Test Application");
      contentPane.setMinimumSize(new Dimension(120, 69));
      contentPane.setPreferredSize(new Dimension(120, 69));
      contentPane.setToolTipText("");
      outputLabel.setText("Output ");
      input2Label.setText("Input    ");
      input1Label.setText("Input    ");
      input1Field.addCaretListener(new javax.swing.event.CaretListener()
      {

         public void caretUpdate(CaretEvent e)
         {
            input1Field_caretUpdate(e);
         }
      });
      input2Field.addCaretListener(new javax.swing.event.CaretListener()
      {

         public void caretUpdate(CaretEvent e)
         {
            input2Field_caretUpdate(e);
         }
      });
      contentPane.add(input1Box, null);
      input1Box.add(input1Label, null);
      input1Box.add(input1Field, null);
      contentPane.add(input2Box, null);
      input2Box.add(input2Label, null);
      input2Box.add(input2Field, null);
      contentPane.add(outputBox, null);
      outputBox.add(outputLabel, null);
      outputBox.add(outputValue, null);
   }

   //Overridden so we can exit when window is closed
   protected void processWindowEvent(WindowEvent e)
   {
      super.processWindowEvent(e);
      if (e.getID() == WindowEvent.WINDOW_CLOSING)
      {
         System.exit(0);
      }
   }

   void Calculate()
   {
      try
      {
         outputValue.setText(String.valueOf(Double.parseDouble(input1Field.getText()) / Double.parseDouble(input2Field.getText())));
      }
      catch (Throwable exception)
      {
      };
   }

   void input1Field_caretUpdate(CaretEvent e)
   {
      Calculate();
   }

   void input2Field_caretUpdate(CaretEvent e)
   {
      Calculate();
   }
}

Copyright © 2004 by Mark Cashman (unless otherwise indicated), All Rights Reserved