Richard G Baldwin (512) 223-4758, baldwin@austin.cc.tx.us, http://www2.austin.cc.tx.us/baldwin/

JDK 1.1, Preserving Default Key Behavior

Java Programming, Lecture Notes # 902, Revised 3/20/97.

Introduction

This is one in a planned series of documents which will be written in response to questions posted on the various Java newsgroups.

In most cases, the response will be in the form of a sample program. In most cases, the programs will be written using JDK 1.1 unless there is some special reason to backtrack to JDK 1.0.2.

Also, in most cases, there will be very little discussion other than the comments contained in the program.

Sample Program

This program was written in response to a request from a poster for code which "propagates key events in JDK 1.1 up the container hierarchy."


/*File news003.java Copyright 1997, R.G.Baldwin
Designed to be compiled and run under JDK 1.1.
Written and tested using JDK 1.1 under Win95.

This program was written in response to a newsgroup poster who
asked how to propagate events up the hierarchy in JDK 1.1 in 
order to preserve default behavior of certain keystrokes.

The poster's explanation for needing to propagate events up the
hierarchy was as follows:

"The main reason that I asked for event propagation is different.  Let 
me clearly explain my situation.  I have developed DateTextField for 
my own use.  This field validates and completes the date if necessary.  
But I am not handling all the keyDown events.  For example HOME, 
END etc.... For these I have been depending on the default behavior.  In 
this case I have to "return false" so that the default behavior is invoked 
and get the work done.  I strongly believe that there may be a way to 
propagate event, because this is one of the reported bugs to JavaSoft.  
If you see where I am getting at, and if you know the answer (I am 
pretty sure you do) PLEASE HELP ME."

DateTextField does not appear in the JDK 1.1 documentation index so we 
might assume that is the name of a class or object defined and created 
by the poster, and that it is based on the TextField component.

This program places an editible TextArea object in the bottom of a Frame
and an editible TextField object in the top of an object.  The program
writes 99 lines of text into the TextArea object and writes a single
line of text into the TextField object.

Two low-level KeyEvent handlers are instantiated and registered to listen
for keyTyped() events on the two Text objects.  The action of the event 
handler is simply to display the key that is typed on the console.

The important point is that without making any special effort to preserve
default handling, the Delete, Home, End, Arrow, and Backspace keys 
behave as would be expected on both Text objects.  The PageUP and 
PageDown keys behave as would be expected on the TextArea object and 
have no meaning on the TextField object since it contains only one line 
of text.  The behavior of the Tab key is a little strange.  It shifts 
the focus when the TextField has the focus, but inserts blank space when 
the TextArea has the focus.
*/

import java.awt.*;
import java.awt.event.*;
//===================================================================
class news003{
  public static void main(String[] args){
    GUI gui = new GUI(); //instantiate a user interface object
  }//end main()
}//end class001
//===================================================================
class GUI{
  GUI(){//constructor
    Frame myFrame = new Frame("Copyright 1997, R.G.Baldwin");
    TextArea myTextArea = new TextArea("Text Line 0\n");
    TextField myTextField = new TextField("Copyright 1997, R.G.Baldwin");     
    
    for(int cnt = 1; cnt <100; cnt++)//add 98 more lines of text
      myTextArea.append("Text Line " + cnt + "");
    myFrame.add("South",myTextArea);//place test area in the Frame
    
    myFrame.add("North",myTextField);

    myFrame.setSize(300,300);
    myFrame.setVisible(true);

    //Instantiate and register a KeyEvent listener object for the 
    // TextArea object and the TextField object.
    myTextArea.addKeyListener(new MyKeyListener());
    myTextField.addKeyListener(new MyKeyListener());
    
    //Instantiate and register program termination object.
    myFrame.addWindowListener(new Terminate());    
  }//end constructor
}//end class GUI
//==========================================================

//Objects of this class listen for keyTyped() events and display
// the character on the console.
class MyKeyListener extends KeyAdapter{
  public void keyTyped(KeyEvent e){
    System.out.println(e.getKeyChar());
  }//end keyTyped()
}//end MyKeyListener

//==========================================================

//Terminates program when the user closes the Frame object.
class Terminate extends WindowAdapter{
  public void windowClosing(WindowEvent e){
    System.exit(0);
  }//end windowClosing()
}//end class Terminate



//==========================================================
-end-