The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.text  [49 examples] > JTextArea  [7 examples]

e983. Modifying Text in a JTextArea Component

The text in a text area can be modified using the method described in e970 Modifying Text in a JTextComponent. However, the text area supports a similar set of convenience editing methods that are demonstrated in this example.
    // Create the text area
    JTextArea ta = new JTextArea("Initial Text");
    
    // Insert some text at the beginning
    int pos = 0;
    ta.insert("some text", pos);
    
    // Insert some text after the 5th character
    pos = 5;
    ta.insert("some text", pos);
    
    // Append some text
    ta.append("some text");
    
    // Replace the first 3 characters with some text
    int start = 0;
    int end = 3;
    ta.replaceRange("new text", start, end);
    
    // Delete the first 5 characters
    start = 0;
    end = 5;
    ta.replaceRange(null, start, end);

 Related Examples
e982. Creating a JTextArea Component
e984. Enumerating the Lines in a JTextArea Component
e985. Setting the Tab Size of a JTextArea Component
e986. Moving the Focus with the TAB Key in a JTextArea Component
e987. Enabling Word-Wrapping and Line-Wrapping in a JTextArea Component
e988. Implementing a Console Window with a JTextArea Component

See also: Actions and Key Bindings    Caret and Selection    Events    JEditorPane    JFormattedTextField    JTextComponent    JTextField    JTextPane    Styles   


© 2002 Addison-Wesley.