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]

e998. Moving the Caret of a JTextComponent

    JTextComponent c = new JTextArea();
    
    // Get position of the caret
    c.getCaretPosition();
    
    // Get the character following the caret
    if (c.getCaretPosition() < c.getDocument().getLength()) {
        try {
            char ch = c.getText(c.getCaretPosition(), 1).charAt(0);
        } catch (BadLocationException e) {
        }
    }
    
    // Move the caret
    int newPosition = 0;
    c.moveCaretPosition(newPosition);
    
    // Set the caret color
    c.setCaretColor(Color.red);

 Related Examples
e999. Setting the Blink Rate of a JTextComponent's Caret
e1000. Using the Selection of a JTextComponent

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


© 2002 Addison-Wesley.