Application Techniques

Using Subforms to Share Globals among Forms

How to use subforms to share global methods, constants, and variables among forms.

About this technique



Java Client Techniques> Dialogs and subforms


You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

See the chapter on programming forms in the Programmer's Guide

The example uses three forms to illustrate the technique:

Creating a subform to contain globals   Top of page

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; 

Notes about the code

Getting a reference to a single subfrmCircle instance   Top of page

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. 
     theOnlyfrmCircle = this; 
  return theOnlyfrmCircle; 

Notes about the code

Accessing a single subfrmCircle instance from any subfrmGlobals instance   Top of page

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); 
     // 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 

Notes about the code

Launching subfrmGlobals forms   Top of page

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) 
      // Show the form, noting its instance number in the title 
        "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 
     catch (Exception e) 

Notes about the code

Copyright © 2000, SilverStream Software, Inc. All rights reserved.