Want more information ?. Try the Web Home Page for this document.

PSPNum Events

PSPNum - like most JavaBeans - generates events that you can use in other components in your Java applet/application. These events follow the 'event listenener' mechanism first established in JDK 1.1.

PSPNum automatically supports a number of events inherited from parent objects (TextField->TextComponent->Component->Object). These include:

The textValueChanged event allows you to monitor for changes to a PSPNum's Text property. In addition, PSPNum allows you to monitor for changes to four other properties (Value, Mask, DigitsBeforeDP and DigitsAfterDP) via the propertyChange event.

This is best illustrated with a simple example as follows:

/**
 * This demo applet shows how an applet reacts to
 * events for a PSPNum which is part of the applet.
 * Requires applet width=900 and height=400, and
 * pspnum1.jar defined via archive option e.g.
 * archive="file:/D:/jdk1.2.2/lib/prospero/pspnum1.jar"
 */
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.beans.*;
import uk.co.prosperosoftware.pspnum.*;

public class test1 extends Applet
    implements TextListener, PropertyChangeListener, FocusListener {
    /* PSPNum used to show generation of events. */
    private PSPNum Num1 = new PSPNum(50,15,2);
    /* TextAre used to show events generated. */
    private TextArea Audit = new TextArea(20,90);
    /* Current Y-axis. */
    private int curY = 35;
    /* X-axis for first non-label controls. */
    private int colX = 210;
    char[] newline = new char[2];
    /* Default constructor - no parameters. */
    public test1() {
        super();
        setLayout(null);
        newline[0] = 0X0A; // line-feed
    }
    /* Initialise applet. */
    public void init() {
        /* Add the PSPNum and it's label to the applet image. */
        Label LabelNum1 = new Label("This PSPNum generates events:",Label.LEFT);
        add(LabelNum1);
        LabelNum1.setBounds(10,curY,180,20);
        add(Num1);
        Num1.setBounds(colX,curY,Num1.getPreferredSize().width,20);
        curY += (Num1.getSize().height + 10);
        /* Change Num1 to have text right-aligned. */
        Num1.setTextAlign(PSPNum.TEXT_ALIGN_RIGHT);
        /* Add the TextArea and it's label to the applet image. */
        Label LabelAudit = new Label("which are listed here:",Label.LEFT);
        add(LabelAudit);
        LabelAudit.setBounds(10,curY,120,20);
        add(Audit);
        Audit.setBounds(colX,curY,Audit.getPreferredSize().width,Audit.getPreferredSize().height);
        /* Register interest of entire applet in events for the PSPNum. */
        Num1.addPropertyChangeListener(this);
        Num1.addTextListener(this);
        Num1.addFocusListener(this);
    }
    /* Process event generated by TextField (parent class of PSPNum) for change of Text. */
    public void textValueChanged(TextEvent e) {
        if (e.getSource().equals(Num1)) {
            Audit.append("Text Event: " + e.toString());
            Audit.append(new String(newline));
        }
    }
    /* Process event generated by PSPNum itself for change of property (Value) */
    public void propertyChange(PropertyChangeEvent e) {
        if (e.getSource().equals(Num1)) {
            Audit.append("Property " + e.getPropertyName()
            + " changed from:  " + e.getOldValue().toString()
            + " to: " + e.getNewValue().toString());
            Audit.append(new String(newline));
        }
    }
    /* Process event generated by TextField (parent class of PSPNum) for focus gained. */
    public void focusGained(FocusEvent e) {
        if (e.getSource().equals(Num1)) {
            Audit.append("Gained Focus:- Value is: "
            + Num1.getValue()
            + " and Text is: "
            + Num1.getText());
            Audit.append(new String(newline));
        }
    }
    /* Process event generated by TextField (parent class of PSPNum) for focus lost. */
    public void focusLost(FocusEvent e) {
        if (e.getSource().equals(Num1)) {
            Audit.append("Lost Focus:- Value is: "
            + Num1.getValue()
            + " and Text is: "
            + Num1.getText());
            Audit.append(new String(newline));
        }
    }
}