In this lab, you will define a JavaBean that exposes properties, methods and events.
In this part, you will write a JavaBean class that exposes properties. You will then use a bean introspection facility to test that your JavaBean exposes the properties correctly.
Create a new Java project named Lab11.
Create a new class named AccountBean in the part1 package. This new class will act as a JavaBean, so it must implement the java.io.Serializable interface.
Edit AccountBean and define the following properties:
Property Name Property Type Access
------------- ------------- ------
accountHolder String read/write
accountNumber int read/write
balance double read/write
discountRate double read/write
discountedBalance double read
Note that the discountedBalance property is computed by multiplying the balance times the discountRate.
You will use a Bean Feature Viewer program to test whether your JavaBean adheres to the JavaBean specification. The Bean Feature Viewer uses reflection and introspection to examine a class -- if your properties are defined correctly, the Bean Feature Viewer will display them in a list.
To fire up the Bean Feature Viewer, follow these steps:
Open a command-prompt window (choose Run from the Windows Start menu and type cmd).
In the command-prompt window, change to the {Lab Installation Directory}/lib directory, then start the Viewer. Assuming the standard lab setup on Windows XP:
c:
cd \javaclass\lib
java -jar bfv.jar
On Windows Vista or later:
c:
cd \Users\username\javaclass\lib
java -jar bfv.jar
The Viewer should start up.
To open your bean, from the Bean Feature Viewer menu, choose File - Open Bean, then, assuming the standard lab setup on Windows XP, enter:
Bean class root directory: C:\javaclass\workspace\Lab11\bin
Fully qualified class name: part1.AccountBean
On Windows Vista or later:
Bean class root directory:
C:\Users\usernamejavaclass\workspace\Lab11\bin
Fully qualified class name: part1.AccountBean
The Viewer should display a list of properties. Please ensure that all of your bean's properties are listed and that their types are correct.
You can click on a property in the Viewer and it will show details about the property in a window pane at the bottom of the Viewer. Please ensure that all of your propreties are correct, especially that discountedBalance is a read-only property.
In this part, you will add code to your AccountBean so that it exposes an event named LargeBalanceEvent. If you do this properly, you will be able to see the new event in the Bean Feature Viewer, which uses reflection and introspection to determine if a bean exposes events.
Define an event object class named LargeBalanceEvent in the part1.event package. Be sure that this class has the proper superclass!
Complete the event class so that it exposes a balance property of type double. The class should have a two-argument constructor that accepts the event source reference and a balance.
Define an event listener interface named LargeBalanceListener in the part1.event package. Be sure that this new interface has the proper super-interface!
Add the following method to the interface:
public void preferredLevelReached(LargeBalanceEvent evt);
Edit the AccountBean class and add code so that the class can add and remove LargeBalance event listeners. Follow these steps:
Define a private field of type Vector named listeners.
Write a public method named addLargeBalanceListener that accepts a LargeBalanceListener reference and stores it in the Vector.
Write a public method named removeLargeBalanceListener that accepts a LargeBalanceListener reference and removes it from the Vector.
Write a private method named fireLargeBalanceEvent that accepts a balance double. This method should clone the Vector in a synchronized block and then iterate through the clone, calling preferredLevelReached on each reference.
Edit the setBalance method. If the updated balance is greater than 10000, fire the LargeBalanceEvent.
Now let's use the Bean Feature Viewer to test to see if you defined the event correctly. Follow these steps:
Reload the bean class into the Bean Feature Viewer.
You should see the largeBalance event listed in the Events category. Click on it to highlight it -- you should see the following details:
Name: largeBalance
Listener interface: part1.event.LargeBalanceListener
Listener interface methods:
- preferredLevelReached
If you successfully made it through the last part, you know that you coded your AccountBean correctly -- if you didn't, the Bean Feature Viewer would not be able to display the bean's properties and event. Coding the bean correctly is the important part of this lab (not coding a test program).
But if you want more confirmation, in this optional part, you will do a bit of Java Swing programming so you can see your bean working in a GUI.
Import the part3.TestGUI program from the lab11 starters folder into your project's src folder.
Then run TestGUI.java as a Java Application -- note that it defines a button with text "Press Me" -- the button currently doesn't do anything when pressed. Close the TestGUI window.
Examine the TestGUI.java source and note that it defines private method named handleClick that is called when the user presses the button. You will put your code here. Follow these steps:
Within handleClick, instantiate an AccountBean using the zero-argument constructor:
AccountBean account = new AccountBean();
Hook up a listener to the largeBalance event:
account.addLargeBalanceListener(new LargeBalanceListener()
{
public void preferredLevelReached(LargeBalanceEvent evt)
{
JOptionPane.showMessageDialog(TestGUI.this,
"Large balance: " + evt.getBalance());
}
}
);
Note that this code displays a message box that shows the "large balance" amount.
Set the balance to a large amount:
account.setBalance(40000);
Run TestGUI.java as a Java Application, then press the button.
You should see the message box! Thus you have demonstrated how to define a JavaBean that exposes properties and events that can be introspected by tools such as the Bean Feature Viewer.