Example usage for javax.swing.text Utilities getWordEnd

List of usage examples for javax.swing.text Utilities getWordEnd

Introduction

In this page you can find the example usage for javax.swing.text Utilities getWordEnd.

Prototype

public static final int getWordEnd(JTextComponent c, int offs) throws BadLocationException 

Source Link

Document

Determines the end of a word for the given location.

Usage

From source file:Main.java

public void actionPerformed(ActionEvent e) {
    JTextComponent comp = getTextComponent(e);
    if (comp == null)
        return;//from   w ww.  j av  a  2s. c o m
    Document doc = comp.getDocument();
    int start = comp.getSelectionStart();
    int end = comp.getSelectionEnd();
    try {
        int left = Utilities.getWordStart(comp, start);
        int right = Utilities.getWordEnd(comp, end);
        String word = doc.getText(left, right - left);
        doc.remove(left, right - left);
        doc.insertString(left, word.toUpperCase(), null);
        comp.setSelectionStart(start);
        comp.setSelectionEnd(end);
    } catch (Exception ble) {
        return;
    }
}

From source file:Main.java

public void actionPerformed(ActionEvent e) {
    JTextComponent comp = getTextComponent(e);
    if (comp == null)
        return;//  ww  w.  j  a v a 2 s.co m
    Document doc = comp.getDocument();
    int start = comp.getSelectionStart();
    int end = comp.getSelectionEnd();
    try {
        int left = Utilities.getWordStart(comp, start);
        int right = Utilities.getWordEnd(comp, end);
        String word = doc.getText(left, right - left);
        doc.remove(left, right - left);
        doc.insertString(left, word.toUpperCase(), null);
        comp.setSelectionStart(start);
        comp.setSelectionEnd(end);
    } catch (BadLocationException ble) {
        return;
    }
}