Here you can find the source of selectLineAtCaret(JTextComponent editor)
public static void selectLineAtCaret(JTextComponent editor)
//package com.java2s; //License from project: Apache License import javax.swing.text.JTextComponent; public class Main { public static void selectLineAtCaret(JTextComponent editor) { int lineStart = getLineStart(editor.getText(), editor.getCaretPosition()); int lineEnd = getLineEnd(editor.getText(), editor.getCaretPosition()); if (lineEnd < editor.getText().length()) { lineEnd++;// w w w. j a va 2s. c o m } editor.getCaret().setDot(lineEnd); editor.getCaret().moveDot(lineStart); } public static int getLineStart(String text, int initialCaretPosition) { int pos = Math.min(Math.max(0, initialCaretPosition), text.length()); while (pos > 0) { if (text.charAt(pos - 1) == '\n') { break; } pos--; } return pos; } public static int getLineEnd(String text, int initialCaretPosition) { int pos = Math.min(Math.max(0, initialCaretPosition), text.length()); while (pos < text.length()) { if (text.charAt(pos) == '\n') { break; } pos++; } return pos; } }