How to extend a SilverStream control and use it on a form.
See the chapters on advanced form techniques and using JAR files in the Programmer's Guide and the JAR Designer in the Tools Guide |
In SilverStream, you can subclass SilverStream and AWT-based form controls to add to or change their functionality. These extendable classes have names that begin with Agc, without a "J".
NOTE You cannot extend Swing-based controls because they are not serializable in JDK 1.2.These non-extendable classes have names that begin with AgcJ.
This example creates the BlueLabel control, which extends the com.sssw.rt.form.AgcLabel
control. It is always blue with white text.
To create a new package and a class that extends AgcLabel:
controls
.
BlueLabel
and specify that it extends com.sssw.rt.form.AgcLabel
. When you're finished with the Wizard, SilverStream displays the Business Object Designer so that you can write the Java code for the class. A package statement is already included in the code.
import java.awt.Color; import com.sssw.rt.gui.AgoFontIdentifier; import com.sssw.rt.form.AgcLabel;
To write the constructor for the BlueLabel control:
Name: BlueLabel
Visibility: public
Return Type: empty (the field must be blank; constructors have no return value)
Parameters: 2
Name: labelName
Type: String
Name: labelText
Type: String
AgoFontIdentifier font; super.setName(labelName); super.setText(labelText); //grooved border super.setBorderStyle(AgcLabel.BORDER_GROOVED); super.setTransparent(false); // set text style normal. super.setTextStyle(AgcLabel.STYLE_NORMAL); // white text super.setTextColor(Color.white); super.setBackgroundColor(Color.blue); super.setJustification(AgcLabel.JUSTIFY_LEFT); super.setWordWrap(false); font = new AgoFontIdentifier("TimesRoman", 0, 14); super.setFontIdentifier(font);
To create a JAR file that contains the control:
BlueLabel
class with the form, in the Form Designer select File>Jar Files and select the JAR file you created.
import controls.BlueLabel;
public BlueLabel testBlueLabel;
// Create the label, set its size, location, and tool tip testBlueLabel = new BlueLabel("testBlueLabel", "Blue Label"); testBlueLabel.setLocation(5,45); testBlueLabel.setSize(100,25); testBlueLabel.setToolTipText("Example Blue Label");
AgfForm.add()
method. The following example uses the this
object reference to specify the current form.
this.add(testBlueLabel);
For the control to respond to events, you must add the appropriate event listener interfaces. This example adds a mouse listener so that you can respond to events involving the label. SilverStream can generate stubs for interface methods when you use this procedure:
java.awt.event.mouseListener
. Select the check box Create stubs for interface methods.
testBlueLabel.addMouseListener(this);
For example, this code for mouseEntered finds out the component for which the event occurred. This is the same sort of code SilverStream generates to dispatch event handling to methods for individual controls. The only response in this simple example is to display a message in the lblRpt control. You can write similar code for mouseClicked, mouseExited, mouseReleased, and mousePressed.
public void mouseEntered(java.awt.event.MouseEvent mouseEvent1) { Object src = mouseEvent1.getSource(); if (src == testBlueLabel) { lblRpt.setText( "mouseEntered " + ((Component) src).getName(); } }
NOTE If other controls on the form must also respond to mouse events, write code for all controls using these event handling methods that you add yourself, rather than write code in the SilverStream-generated event handlers. For information, see the section on handling events for controls instantiated at runtime in the Advanced Form Topics chapter of the Programmer's Guide.