How to use subforms to share global methods, constants, and variables among forms.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on programming forms in the Programmer's Guide |
The example uses three forms to illustrate the technique:
initfrmCircle()
that returns a reference to its first instance. This method replaces each subfrmCircle instance that calls it (except the first) with the first subfrmCircle instance.
formLoaded()
method calls the Circle's initfrmCircle()
method to reference the first instance. This ensures that all Globals instances access the constants and variables of the same Circle instance (the first).
The following code declares a global counter variable, a global constant, and a reference to itself (subfrmCircle).
//Declarations from subfrmCircle // A reference to this subFrom, but only the first instance private static subfrmCircle theOnlyfrmCircle; // A Global counter variable (modifiable) int counter = 0; // A Global constant static final double PI = 3.14159;
initfrmCircle()
method.
The following code shows the method in subfrmCircle that returns a reference to the first instance. The first time it is called, initfrmCircle()
sets the OnlyfrmCircle global variable to itself and returns it. In subsequent executions of this method, it returns OnlyfrmCircle.
public subfrmCircle initfrmCircle() { // Sets the variable to reference the very first instance of // this class. In this way, any other Globals form that calls // this method will be refer to the same (the first) // Circle instance. This permits only one instance of // subfrmCircle to be shared across forms, like a global variable. if(theOnlyfrmCircle==null) theOnlyfrmCircle = this; return theOnlyfrmCircle; }
formloaded()
method in subfrmGlobals uses initfrmCircle()
to get the first subfrmCircle instance, then assigns it to the calling subfrmCircle instance.
The following code shows how to set a new instance of a subfrmCircle object to the first instance of the subfrmCircle object. Other methods in subfrmGlobals show how to get the PI constant and increment the counter. These global variables are accessed by all subfrmGlobals instances.
//formLoaded code in subfrmGlobals protected void formLoaded() { nfmtFormat.setMaximumFractionDigits (5); fldRadius.setText("2"); // Get the reference to the single, "global" instance // of the subForm subfrmCircle = subfrmCircle.initfrmCircle(); }
This is code from the Show PI button in subfrmGlobals:
private void handle_btnShowPI_actionPerformed(ActionEvent evt) { // Get the value of the constant PI from frmCircle fldPi.setText(nfmtFormat.format (subfrmCircle.PI)); } private void handle_btnCounter_actionPerformed(ActionEvent evt) { // Increment the variable counter in the one and only frmCircle subfrmCircle.m_counter++; fldCounter.setText(String.valueOf(subfrmCircle.m_counter)); }
initfrmCircle()
and setting it to the returned value.
formLoaded()
is called when the form is loaded, the new subfrmCircle instance is already reassigned to the first instance. Therefore the PI variable is the "global" constant that all subfrmGlobals instances access.
The following code launches a Globals subform with each button click. These forms are subfrmGlobals instances. Each instance is represented by an integer that is displayed in the title bar of the form's window.
//General declarations in frmGlobalsLauncher final static String SUBFORM_NAME = "subfrmGlobals"; int m_instance = 1;
The following shows the code for the launch subfrom button in frmGlobalsLauncher:l
private void handle_btnLaunch_actionPerformed(ActionEvent evt) { try { // Show the form, noting its instance number in the title agDialog.showForm( "Instance "+ String.valueOf(m_instance), //title agDialog.getForm(SUBFORM_NAME), // form name 2, // window type false, // resizable -1, // x position -1); // y position // increment our instance counter m_instance++; } catch (Exception e) { agDialog.displayError(e); } }
getForm()
and supplying subfrmGlobals as a parameter.
showForm()
. Note that the subfrmGlobals instance is displayed with the m_instance variable.