The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.text  [49 examples] > Caret and Selection  [3 examples]

e1000. Using the Selection of a JTextComponent

    JTextComponent c = new JTextArea();
    
    // Get text inside selection
    c.getSelectedText();
    
    // Replace selected text
    c.replaceSelection("replacement text");
    
    // Set the start of the selection; ignored if new start is < end
    c.setSelectionStart(10);
    
    // Set the end of the selection; ignored if new end is > start
    c.setSelectionEnd(20);
    
    // Better way to set the selection
    c.select(10, 20);
    
    // Set the color of text inside the selection
    c.setSelectedTextColor(Color.red);
    
    // Set the color behind the selected text
    c.setSelectionColor(Color.green);

 Related Examples
e998. Moving the Caret of a JTextComponent
e999. Setting the Blink Rate of a JTextComponent's Caret

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


© 2002 Addison-Wesley.