/* -----BEGIN PGP SIGNED MESSAGE----- */ /** How to make text wrap in a java.awt.TextArea */ /** See method forceTextAreaWrap(..) Sample applet code */ /* new code by Tony Dahlman = freeware */ /* See Listing 17.22. TextFields and TextAreas in action at */ /* www.developer.com/reference/library/1575211297/ch17.htm */ /* thanks to "various authors" & EarthWeb for posting their book */ /* online! Java Developers Reference -- Publication Date: Oct-96 */ /* ISBN: 1-57521-129-7 Copyright 1996, 1997 EarthWeb, Inc. */ /* All Rights Reserved. Okay for personal, non-commercial use. */ import java.awt.*; import java.applet.Applet; public class TextStuff extends Applet { TextArea text_space; TextArea selection; Button show_selection, make_selection; TextField start, stop; public void init() { int i; String sample_text; sample_text = "This is a very long piece of text which is " +"designed to show how " +"a text area can hold a lot of text without you having to do a lot " +"of work. " +"In general text areas are good for holding large chunks of text or " +"for getting " +"long answers back from the user. TextAreas have lots of methods for " +"manipulating their contents and for controlling how the text is " +"scrolled. Some are illustrated in this modified version's method, " +"\"forceTextAreaWrap(TextArea,int)\". Compile and run the original " +"then erase the comment slashes at the left margin of the source " +"to see what wrapping your TextArea can do..." ; //define the TextArea text_space = new TextArea(sample_text,8,50); // This line added to textbook applet..................................... // insert newline chars to improve display forceTextAreaWrap(text_space,60); //define the report TextArea selection = new TextArea("",10,50); //create the button to show the selection values show_selection = new Button("Selection Report"); //create the text field to input the start of the selection start = new TextField("0",2); //create the text field to input the end of the selection stop = new TextField("0",2); //make the labels for the two input fields Label l_start = new Label("Start of selection"); Label l_stop = new Label("End of selection"); //define the button to make the selection make_selection = new Button("Make selection"); //add everything to the applet's panel add(text_space); add(show_selection); add(selection); add(l_start); add(start); add(l_stop); add(stop); add(make_selection); } // init() // This method added to the textbook applet.................................... // Your comments, suggestions welcome: public void forceTextAreaWrap(TextArea txt, int wrapAmount) { String newline; try { newline = System.getProperty("line.separator","\n"); } catch ( SecurityException e) { newline = "\n"; System.out.println("SecurityException caught getting" +" \"line.separator\" property."); } int wrapHere = wrapAmount; int lineStart = 0; while ( wrapHere < txt.getText().length() ) { String substr = new String( txt.getText().substring(lineStart,wrapHere) ); int lastNewLine = substr.lastIndexOf(newline); int lastSpace = substr.lastIndexOf(' '); // if there's already a newline char then leave it alone and continue! if ( lastNewLine > 0 ) { wrapHere = lineStart + lastNewLine + 1; // otherwise find the last space and replace it with a newline char } else { if ( lastSpace > 0 ) { wrapHere = lineStart + lastSpace; txt.replaceRange(newline, wrapHere, wrapHere + 1); } } lineStart = wrapHere + 1; wrapHere += wrapAmount; } } // forceTextAreaWrap(..) // this is 1.02 style event handling, ignore the deprecated API warnings // handle mouse clicks on the two buttons public boolean action (Event e, Object o) { int start_location,stop_location; String report, temp; if (e.target instanceof Button) { //check the button label to decide which button was clicked if (((String)o).equals("Make selection")) { //read the text from the text field to //get the start of the selection temp = start.getText(); //convert the text to an integer start_location = Integer.parseInt(temp); //get the text from the text field to //define the end of the selection temp = stop.getText(); //convert the text to an integer stop_location = Integer.parseInt(temp); //set the selection to the interval defined by //the values in the text fields text_space.select(start_location, stop_location); } else { report = "Selection Start = "; //get the start of the current selection report = report + text_space.getSelectionStart() + "\n"; //get the end of the current selection report = report + "Selection End = "; report = report + text_space.getSelectionEnd() + "\n"; //get the selected text report = report + "Selected Text is: "; // Next two lines of code added to textbook applet code............ // remove the line-feeds String selected = new String( text_space.getSelectedText() ); selected = selected.replace('\n',' '); report = report + selected + "\n"; //put the report in the text area selection.setText(report); // Here is call to the TextArea wrap method in a different context // wrap text for better display forceTextAreaWrap(selection,60); } // if "Make selection" } // if instance of Button return true; } // action(..) } // end class TextStuff /* Here is a TextStuff.html file to be called from your applet viewer:


*/ /* check my signature (Anton Dahlman) if you like at: http://www.four11.com -or- http://pgp5.ai.mit.edu/ -----BEGIN PGP SIGNATURE----- Version: 2.6.2 iQCVAwUBNTrCiWbsFmrW0oYFAQEPDQP/YrOECdwhWQ1TWVAaTL9g8v+onGvACrpU O8LH6bXZ1oZQ3ec0VqPjRt5OYsoCw3HcOXKcr22AtFngumy80HKo3c4KgPi/ZNjy wxR1GcwJpjbEJf+ZjRrRI7LJpAG1ZkZAq+nm9tV4Q33TziRCTfXrkINW1yxBi0/R fXFhhryWlCo= =jVaE -----END PGP SIGNATURE----- */