package awtExtExamples ; /* * * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. Please refer to the file "copyright.html" * for further important copyright and licensing information. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ import java.awt.Panel; import java.awt.GridLayout; import java.awt.Event; import java.awt.Label; import java.awt.Color; import awtExt.*; import sysExt.*; /** * TextFieldTest demonstrates how to control keyboard input * in a TextField. Two methods are registered with the phone number * TextField's eventDispatch object, one method for KEY_ACTION events * and one for KEY_PRESS events. The KEY_PRESS event handler method is a * simple filter that only accepts numeric chars and the '-' chars. * The KEY_ACTION event handler method simply prints to stdout that * it was invoked. * Buttons on the bottom of the window control a few aspects of the * TextFields, such as clearing , resetting , and printing their content. * Note: selecting text programmatically is broken in this JDK release * */ public class TextFieldTest extends Frame { TextField t1; TextField t2; TextField t3; TextField t4; public TextFieldTest() { super("TextFieldTest"); try { Panel p = new Panel(); p.setLayout(new GridLayout(0, 2)); p.add(new Label("name")); p.add(t1 = new TextField("Beatrix Butterfly")); t1.setForeground(Color.blue); p.add(new Label("address")); p.add(t2 = new TextField("3 Green Fields Lane")); p.add(new Label("city")); p.add(t3 = new TextField("SunnyHills")); p.add(new Label("PhoneNum")); p.add(t4 = new TextField("718-555-0144")); t4.setForeground(Color.red); /* ** Register the two event handling methods */ t4.eventDispatch.KEY_ACTION= Callback.newRef(this, "keyActionMethod" ); t4.eventDispatch.KEY_PRESS = Callback.newRef(this, "keyPressMethod" ); add("Center", p); p = new Panel(); Button b; p.add(new Button("clear", this, "clearMethod")); p.add(new Button("reset", this, "resetMethod")); p.add(new Button("select",this, "selectMethod")); p.add(new Button("print", this, "printMethod")); add("South", p); move(200, 100); pack(); show(); t1.selectAll(); } catch( Exception e ) { e.printStackTrace();} } /**------------------------------------------------- * This method gets called back from the phone # text field, * whenever a function key is pressed. Doesn't do anything. **/ public void keyActionMethod(CallbackInfo cbi ) { System.out.println("in keyAction..."); } /**------------------------------------------------- * This method gets called back from the phone # text field, * whenever a key is pressed. It is a simple filter * that allows only numeric key input (and the dash). **/ public void keyPressMethod(CallbackInfo cbi ) { char key = (char)cbi.evt.key; System.out.println("in keyPress...char(" + cbi.evt.key + ") = " + key); if( (key >= '0' && key <= '9') || key == '-' || key == '\b' || key == (char)127) cbi.processed = false ; else cbi.processed = true ; } /**------------------------------------------------- * This method gets called back from the clear button... **/ public void clearMethod(CallbackInfo cbi ) { t1.setText(""); t2.setText(""); t3.setText(""); t4.setText(""); } /**------------------------------------------------- * This method gets called back from the reset button... **/ public void resetMethod(CallbackInfo cbi ) { t1.setText("Beatrix Butterfly"); t2.setText("3 Green Fields Lane"); t3.setText("SunnyHills"); t4.setText("718-555-0144"); } /**------------------------------------------------- * This method gets called back from the select button... **/ public void selectMethod(CallbackInfo cbi ) { System.out.println("in Select"); t1.selectAll(); t2.selectAll(); t3.selectAll(); t4.selectAll(); } /**------------------------------------------------- * This method gets called back from the print button... **/ public void printMethod(CallbackInfo cbi ) { System.out.println("-- values --"); System.out.println("name=" + t1.getText()); System.out.println("address=" + t2.getText()); System.out.println("city=" + t3.getText()); System.out.println("number=" + t4.getText()); } public static void main(String args[]) { new TextFieldTest(); } }