Working with Basic Application Beans


Introduction

Basic Application Beans (BAB) are based on Swing.  BAB implements the Model - View - Controller (MVC) design pattern into UI beans (view/controller) and non-UI beans (model).  With this design, you can save coding time because one model bean can provide input to several view/controller beans.  For example,  a financial report application could have one model bean that contains all the data for several departments.  The view/controller bean for each department presents the data received from the model in the format required for that department.

With an IDE, such as VisualAge for Java, and BAB, you can quickly create a GUI using:  

Using the BABApp Bean

The BABApp bean is the controller for all of the other Basic Application Beans. As you add beans to your application, the BABApp bean provides:

You can only have one instance of a BABApp in an application.  To implement BABApp, you must call BABApp's init method before making the frame visible.  In VisualAge for Java, you call:

 (getMyApp().init()

The call is made from the end of the initialize method generated by VisualAge for Java.  

For JBuilder, call: 

 init()

on your BABApp before the frame is packed and setVisible(true) is called on the frame.

If you use a model in your application and you use BAB's SaveAction, whenever the model's properties are changed.you must call:

 BABApp.modelChanged();

If you use BAB's default behavior for NewAction or OpenAction, you must listen for NewModelEvent from BABApp.  Point your instance of the model to the model maintained by BABApp.  BABApp creates a new instance and fires a NewModelEvent whenever the user performs NewAction or OpenAction. 

If you get an exception because a field is not serializable when you save your application data, the probable reason is that the event listenner list in your model is not transient and the listeners are being saved with the model data.  To fix this:

BMenuItem

BMenuitem is a modified JMenuItem.  Its properties are initialized from a BAction bean.  To use a BMenuItem, you add it to a menu and then use targetAction to connect with a BAction bean such as FileOpen. 

BButton

BButton extends JButton.  BButton works with an associated BAction bean to perform the task represented by the BButton.  To use the BButton you add it to your GUI and than use targetAction to connect it with a BAction bean such as ToggleStatusBarAction bean.  Some of the BButton properties you can set are:

     

BAction

Baction extends Swing action. To set the action performed by BAction, you add an ActionListener or connect it to a bean such as a BButton using targetAction. If you want to implement an undoable action/command then a BCommand object must be implemented and the command itself must added to the BABApp in the ActionListener.  BAction beans can be connected to to several buttons or menu items requiring the same action such as add or save. You can write your own code to customize a BAction.  If you use a Swing button other than BButton, you must use an Action Enabler connected between the button and Baction.  BAB offers the following specific purpose action beans:

  BCommand

BCommand is a modified UndoableEdit from Swing.  You can use BCommand as undoable or not undoable command.  If set to undoable, BAB provides the infrastructure for the undo/redo of the command.

BImageIcon

BImageIcon extends ImageIcon into a component. Use BImageIcon to load images from a Jar file or from a relative directory for either applications or applets.  You must use either an image file name or image URL string to initialize this Icon. If an attempt is made to set both, only the last set operation will take effect.

StatusBar

StausBar extends JPanel. You use StausBar to display messages about the state of the application. Because the messages are stacked, you can display previously set messages. The statusbar requires a JLabel or another such component to display messages. You can customize the StatusBar to contain other components. If you do not set a display component, the statusbar adds a JLabel to the center of the JPanel and this is used for displaying the messages.

Model

Model extends Object.  You use Model as the container for the data used by your application.  When the application's data is changed, Model receives the change and then passes the new data to the associated view.