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.Event; import java.awt.Panel; import java.awt.Color; import java.awt.Font; import awtExt.* ; /** * TextAreaTest demonstrates how to control keyboard input * in a TextArea. Two methods are registered with the TextArea'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 toggles the case of a letter. The KEY_ACTION event handler * method toggles the letters' case back if the funtcion key F5 is pressed. * Buttons on the bottom of the window control a few aspects of the * TextArea, such as clearing it, resetting it, and printing its content. * Note: selecting text programmatically is broken in this JDK release. */ public class TextAreaTest extends Frame { TextArea t1; TextArea t2; public TextAreaTest() { super("TextAreaTest"); try { add("North", t1 = new TextArea(4, 40)); t1.setForeground(Color.blue); t1.setEditable(false); t1.setText( "Type a few words and press F5\n when finished..."); add("Center", t2 = new TextArea(10, 40)); t2.setFont(new Font("Helvetica", Font.BOLD, 18)); /* ** Register the two event handling methods */ t2.eventDispatch.KEY_ACTION = Callback.newRef(this,"keyActionMethod"); t2.eventDispatch.KEY_PRESS = Callback.newRef(this,"keyPressMethod" ); Panel p = new Panel(); 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(400, 200); pack(); show(); } catch( Exception e ) { e.printStackTrace() ; } ; } /**----------------------------------------------- * This method gets called back from the text area, * whenever a function key is pressed. If the function key * is F5, it'll toggle the letters case. **/ public void keyActionMethod(CallbackInfo cbi ) { char functionKey = (char)cbi.evt.key; if( functionKey == Event.F5 ) { String s = t2.getText() ; StringBuffer sb = new StringBuffer() ; for( int i = 0 ; i < s.length() ; i++ ) { char key = s.charAt(i); if( key >= 'a' && key <= 'z') { key -= (int)' ' ; } else if (key >= 'A' && key <= 'Z') { key += (int)' ' ; } sb.append( key ); } t2.setText( sb.toString() ) ; } } /**------------------------------------------------- * This method gets called back from the text area, * whenever a key is pressed. It is a simple filter * that toggles the letters case. **/ public void keyPressMethod(CallbackInfo cbi ) { char key = (char)cbi.evt.key; if( key >= 'a' && key <= 'z') { cbi.evt.key -= (int)' ' ; } else if (key >= 'A' && key <= 'Z') { cbi.evt.key += (int)' ' ; } } /**------------------------------------------------- * This method gets called back from the reset button... **/ public void clearMethod(CallbackInfo cbi ) { t1.setText(""); t2.setText(""); } /**------------------------------------------------- * This method gets called back from the reset button... **/ public void resetMethod(CallbackInfo cbi ) { t2.setText("TRY AGAIN...."); } /**------------------------------------------------- * This method gets called back from the select button... **/ public void selectMethod(CallbackInfo cbi ) { t2.setText( "Sorry! select is broken in this\nversion of awt..." ) ; t2.selectAll(); } /**------------------------------------------------- * This method gets called back from the print button... **/ public void printMethod(CallbackInfo cbi ) { System.out.println("-- values --"); System.out.println("text=" + t1.getText()); System.out.println("text=" + t2.getText()); } public static void main(String args[]) { new TextAreaTest(); } }