Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
 
Training Index

Java Beans Tutorial, Part 4
JDK 1.1 Event Handlers for Beans

By Greg Voss, JavaSoft

[Tutorial Contents]

In the following stepse, you will add JDK 1.1 event handler methods to NervousText so that you can program NervousText beans to change their run time (as opposed to design time) behavior. While defining properties allows you to change the way a bean looks or behaves while building an application, event handlers allow you to define methods which can be fired in a running application.

Start by adding adding an event receiver so that you can change the direction the text is written in NervousText from the OurButton object. OurButton is already available in the beanbox.

Step 1. Add import Statement for AWT events

First add the JDK 1.1 event handler import statement after import java.awt.*;

import java.awt.event.*;
You should now have the following package and import statments at the top of NervousText02.java:
package sun.beanbox.beans;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.*;

Step 2. Add a Variable to Control Direction of Text

Add a boolean toggle variable called lefttoright in the NervousText class after the line: booleanthreadSuspended = false

boolean lefttoright=true;
The data field declartions for NervousText02 should now look like this:
public class NervousText02 extends Panel 
  implements Runnable, MouseListener {

  char separated[];
    String s = null;
    Thread killme = null;
    int i;
    int x_coord = 0, y_coord = 0;
    String num;
    int speed=35;
    int counter =0;
    boolean threadSuspended = false; //added by kwalrath
    boolean lefttoright = true;
    ...

Step 3. Create ActionEvent Handler Method

Create an action event receiver target called changeDirection that toggles the state of lefttoright. The beanbox has a button called OurButton that fires off ActionEvent's. It will find changeDirection through introspection. Add this method immediatel after the getText property setter:

public void changeDirection(ActionEvent x)
{
  if(lefttoright)
    lefttoright=false;
  else
    lefttoright=true;
}

Step 4. Modify NervousText's paint Method

Finally change the paint method to draw the nervous text in a different direction depending on the value of lefttoright. To do this, change the call to drawChars in the paint method from:

   g.drawChars(separated, i,1,x_coord,y_coord);
to:

  if(lefttoright)
    g.drawChars(separated, i,1,x_coord,y_coord);
  else
    g.drawChars(
      separated, (s.length())-i-1,1,x_coord,y_coord);
The paint method now looks like this
public void paint(Graphics g) {
  for(i=0;i < S.LENGTH();I++)
  {
    X_COORD = (INT) (MATH.RANDOM()*10+15*I);
    Y_COORD = (INT) (MATH.RANDOM()*10+36);
    IF(LEFTTORIGHT)
      G.DRAWCHARS(SEPARATED, I,1,X_COORD,Y_COORD);
    ELSE
      G.DRAWCHARS(
        SEPARATED, (S.LENGTH())-I-1,1,X_COORD,Y_COORD);
  }
}

Step 5. Build the JAR and Install in the BeanBox

Compile NervousText02.java:

   javac -d . NervousText02.java

Create the mainfest:

   echo "Manifest-Version: 1.0" > manifest.tmp
   echo "" >> manifest.tmp
   echo "Name: sun/beanbox/beans/NervousText02.class" 
     >> manifest.tmp
   echo "Java-Bean: True" >> manifest.tmp
The resulting temporary manifest file should look like this:
   Manifest-Version: 1.0

   Name: sun/beanbox/beans/NervousText02.class
   Java-Bean: True
Create the JAR file:
   jar cfm NervousText02.jar manifest.tmp 
     sun/beanbox/beans/NervousText02.class

Remove the temporary manifest and install the JAR in the BeanBox jars directory (substituting your BDK installation directory for BDK_HOME:

   rm manifest.tmp
   cp -p NervousText02.jar BDK_HOME/beans/jars

Step 6. Test NervousText02 in BeanBox

You'll be adding two beans to the BeanBox for this test: OurButton and NervousText02.

To run the BeanBox, switch to the BDK_HOME/beans/beanbox directory. Invoke run.sh (UNIX) or run.bat (Window/DOS). Select an OurButton bean from the menu and place it in the BeanBox. Select a NervousText02 bean from the menu, placing it in the BeanBox below the button.

Now you want a mouse press on the OurButton bean to fire an actionPerformed button event. Use the BeanBox Edit menu to direct this action event to be handled by the changeDirection method in NervousText02. Start by making sure the OurButton bean is selected; it will be surrounded by a dotted-line box when you click on it, indicating it is the currently selected bean. A property sheet for OurButton will also be displayed.

Now use the Edit menu to choose Edit->Events->action->actionPerformed. Join the event line to NervousText02 by clicking over the NervousText02 component.

You should then get a popup named EventTargetDialog. Select the changeDirection method and click on the OK button. Pressing OurButton should now cause the text displayed by the NervousText02 button to be reversed from right to left.


Program Source Code

The makefile for this lesson automates source code compilation, JAR file construction, and copying of JAR files to the appropriate BeanBox directory. You'll have to edit several of the variables in the makefile to indicate the location of your JDK 1.1 and BDK installation directories.

You may want to look at the final source file for NervousText Bean, Version 02 to verify the changes you have made to the original NervousText.java file.


[ This page was updated: 5-Nov-99 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.