Close

JTextComponent - Getting word at caret position

[Updated: Jul 4, 2018, Created: Jun 6, 2018]

Following example shows how to get word at caret position. The example creates a JPopupMenu with a test menu item which on clicking prints the word at current caret position.

package com.logicbig.example;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import javax.swing.text.Utilities;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class TextComponentWordAtCaretUtil {

  public static void main(String[] args) {
      JTextArea ta = new JTextArea(5, 5);
      ta.addMouseListener(createPopupListener(ta));
      JFrame frame = createFrame("JTextComponent word at caret example");
      frame.add(new JScrollPane(ta));
      frame.setVisible(true);
  }

  private static MouseListener createPopupListener(JTextComponent tc) {
      JPopupMenu popupMenu = new JPopupMenu();
      JMenuItem menu = popupMenu.add(new JMenuItem("Test"));
      popupMenu.add(menu);
      menu.addActionListener(e -> {
          String word = getWordAtCaret(tc);
          System.out.println(word);

      });
      return new MouseAdapter() {
          @Override
          public void mouseReleased(MouseEvent e) {
              if (e.isPopupTrigger()) {
                  int rightClickPosition = tc.viewToModel2D(e.getPoint());
                  tc.setCaretPosition(rightClickPosition);
                  popupMenu.show(tc, e.getX(), e.getY());
              }
          }
      };
  }

  private static String getWordAtCaret(JTextComponent tc) {
      try {
          int caretPosition = tc.getCaretPosition();
          int start = Utilities.getWordStart(tc, caretPosition);
          int end = Utilities.getWordEnd(tc, caretPosition);
          return tc.getText(start, end - start);
      } catch (BadLocationException e) {
          System.err.println(e);
      }

      return null;
  }

  public static JFrame createFrame(String title) {
      JFrame frame = new JFrame(title);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(new Dimension(800, 700));
      return frame;
  }
}

Console output:

produce

Example Project

Dependencies and Technologies Used:

  • JDK 10
  • Maven 3.3.9

Getting word at caret position Select All Download
  • text-component-word-at-caret
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • TextComponentWordAtCaretUtil.java

    See Also