|
A Java Bean is a special kind of class - self contained, capable of being dropped
on a frame or a data module. They are the next level of reusability above the
class.
Creating a new bean is relatively simple. In this case we will create a bean
that encapsulates the calculation we performed in the most basic
Java application. Here it is...
/**
* Title: Bean to perform division and show
the structure of a bean<p>
* Description: Pushes the basic application functionality into a bean for reuse.<p>
* Copyright: Copyright (c) Mark Cashman<p>
* Company: <p>
* @author Mark Cashman
* @version 1.0
*/
package divisionbean;
import java.awt.*;
import java.lang.Object;
import java.io.*;
public class Divider extends Object{
BorderLayout borderLayout1 = new BorderLayout();
private String myInput1 = "";
private String myInput2 = "";
Divider divider1 = new Divider();
public Divider()
{
try
{
jbInit();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
private void jbInit() throws Exception
{
}
public String getInput1()
{
return myInput1;
}
public void setInput1(String theInput1)
{
myInput1 = theInput1;
}
public String getInput2()
{
return myInput2;
}
public void setInput2(String theInput2)
{
myInput2 = theInput2;
}
public String getResult()
{
String outputValue = "";
try
{
outputValue = (String.valueOf(Double.parseDouble(myInput1)
/ Double.parseDouble(myInput2)));
return outputValue;
}
catch (Throwable exception)
{
return "";
};
}
}
Beans offer an interface that consists of properties (represented by getter
and setter functions), methods, and events. This bean has three properties -
input1,input2, and result, no methods and no events.
The bean is created with the bean wizard (File | New | Object | Java Bean).
The bean can be based on any class - Object, JComponent, whatever. This bean
is based on Object. It's a good idea to have it create a sample property, if
you've never added any. But you can also use the Bean | Property page to add,
change or remove properties. Obviously you need to save it somewhere. Pick your
spot. Then compile.
The hard part is adding the bean to the component palette. You may want to
use a painting tool to create an icon or set of icons and add them to the bean
before adding the bean to the palette. But, regardless of whether or not you
do that, you can then add it to the palette by picking "properties"
from the right button menu on the palette itself, or picking Tools | Configure
Palette. Just make sure to pick and open the package and then pick the bean.
Once the bean is added to the palette, you can create an application to use
it. In this case, we're going to make a copy of the original project frame and
modify it to use the bean.
Don't be tempted to just copy the project. This can be done, but there are
name dependencies. If you do this, copy the original project directory to a
new name, open it in JBuilder, save the frame and the application under new
names (in this case the package is basicbeanapplication). But that isn't enough.
It won't run. You will then need to edit the .jpr file with notepad and change
the package name and the paths to your new package and paths.
Or, you can just recreate the project from scratch.
In any event, here's the revised frame...
/**
* Title: Basic Bean Application<p>
* Description: Reproduces the similar C++ Builder example allowing
"calculator-like" functions, demonstrating exception handling, input and output,
using a bean.<p>
* Copyright: Copyright (c) Mark Cashman<p>
* Company: <p>
* @author Mark Cashman
* @version 1.0
*/
package basicbeanapplication;
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();
NonVisual nonVisual = new NonVisual();
//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(nonVisual.divider.getResult());
}
catch (Throwable exception)
{
};
}
void input1Field_caretUpdate(CaretEvent e)
{
nonVisual.divider.setInput1(input1Field.getText());
Calculate();
}
void input2Field_caretUpdate(CaretEvent e)
{
nonVisual.divider.setInput2(input2Field.getText());
Calculate();
}
}
The application not only has a frame, but also a new data module for the non-visual
"divider" bean, called "nonVisual". You can see the instantiation
of the data module as
NonVisual nonVisual = new NonVisual();
right at the top of the frame.
Notice that the new structure is a little simpler, and a little more separated.
And note that the processing is now completely separate from the user interface.
We could feed to bean from an expression parser, from a spreadsheet, or from
an automated data gathering device.
Keep this in mind. It's one of the best reasons for creating a non-visual bean.
|