The Java Developers Almanac 1.4


Order this book from Amazon.

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

e972. Finding Word Boundaries in a JTextComponent

This example demonstrates an action that selects the word adjacent to the caret. If the caret is adjacent to two words, the word following the caret is selected. The implementation uses BreakIterator.getWordInstance() to find word boundaries.
    public TextAction selectWordAction = new TextAction("Select Word") {
        public void actionPerformed(ActionEvent evt) {
            JTextComponent c = getTextComponent(evt);
            if (c == null) {
                return;
            }
            int pos = c.getCaretPosition();
    
            try {
                // Find start of word from caret
                int start = Utilities.getWordStart(c, pos);
    
                // Check if start precedes whitespace
                if (start < c.getDocument().getLength()
                    && Character.isWhitespace(c.getDocument().getText(start, 1).charAt(0))) {
                    // Check if caret is at end of word
                    if (pos > 0
                        && !Character.isWhitespace(c.getDocument().getText(pos-1, 1).charAt(0))) {
                        // Start searching before the caret
                        start = Utilities.getWordStart(c, pos-1);
                    } else {
                        // Caret is not adjacent to a word
                        start = -1;
                    }
                }
                if (start != -1) {
                    // A non-whitespace character follows start.
                    // Find end of word from start.
                    int end = Utilities.getWordEnd(c, start);
    
                    // Set selection
                    c.setSelectionStart(start);
                    c.setSelectionEnd(end);
                }
            } catch (BadLocationException e) {
            }
        }
    };
    
    public TextAction selectNextWordAction = new TextAction("Select Next Word") {
        public void actionPerformed(ActionEvent evt) {
            JTextComponent c = getTextComponent(evt);
            if (c == null) {
                return;
            }
            int pos = c.getSelectionStart();
            int len = c.getDocument().getLength();
    
            try {
                // Find start of next word from selection.
                // getNextWord() throws BadLocationException if no word follows pos.
                int start = Utilities.getNextWord(c, pos);
    
                // Find end of word from start
                int end = Utilities.getWordEnd(c, start);
    
                // Set selection
                c.setSelectionStart(start);
                c.setSelectionEnd(end);
            } catch (BadLocationException e) {
            }
        }
    };
    
    public TextAction selectPrevWordAction = new TextAction("Select Previous Word") {
        public void actionPerformed(ActionEvent evt) {
            JTextComponent c = getTextComponent(evt);
            if (c == null) {
                return;
            }
            int pos = c.getSelectionStart();
            int len = c.getDocument().getLength();
    
            try {
                // Find start of previous word from selection.
                // getPreviousWord() throws BadLocationException if no word precedes pos.
                int start = Utilities.getPreviousWord(c, pos);
    
                // Find end of word from start
                int end = Utilities.getWordEnd(c, start);
    
                // Set selection
                c.setSelectionStart(start);
                c.setSelectionEnd(end);
            } catch (BadLocationException e) {
            }
        }
    };

 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
e973. Retrieving the Visible Lines in a JTextComponent
e974. Using a Position 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.