The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.text  [49 examples] > JTextComponent  [11 examples]

e974. Using a Position in a JTextComponent

A position marks a location between two characters in a JTextComponent. The position attempts to remain between the same two characters despite edits to the JTextComponent. For example, if the position is at location 10 and the first character is deleted, the new location of the position becomes 9.

When text is inserted at the position, a position's bias determines whether it remains in the same location or is moved to the end of the new text. By default, createPosition() returns a position with a right bias, which means inserting text at the position causes it to move to the end of the inserted text. There is no way to change the bias at present.

    JTextComponent textComp = new JTextArea();
    Document doc = textComp.getDocument();
    
    Position p = null;
    try {
        // Create a right-bias Position object
        int location = 3;
        p = doc.createPosition(location);
    } catch (BadLocationException e) {
    }
    
    // Retrieve the current location of the Position object
    int location = p.getOffset();

 Related Examples
e968. Retrieving Text from a JTextComponent
e969. Retrieving All the Text from a JTextComponent Efficiently
e970. Modifying Text in a JTextComponent
e971. Asynchronously Reading the Contents of a Visible JTextComponent
e972. Finding Word Boundaries in a JTextComponent
e973. Retrieving the Visible Lines in a JTextComponent
e975. Limiting the Capacity of a JTextComponent
e976. Enabling Text-Dragging on a JTextComponent
e977. Sharing a Document Between JTextComponents
e978. Enumerating All the Views in a JTextComponent

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


© 2002 Addison-Wesley.