Here you can find the source of getColumnAtPosition(JTextComponent editor, int caretPosition)
public static int getColumnAtPosition(JTextComponent editor, int caretPosition)
//package com.java2s; //License from project: Apache License import javax.swing.text.BadLocationException; import javax.swing.text.JTextComponent; public class Main { public static int getColumnAtPosition(JTextComponent editor, int caretPosition) { if (editor.getDocument().getLength() == 0) { return 0; }/*from www . ja v a2 s. c o m*/ if (caretPosition >= editor.getDocument().getLength() - 1) { caretPosition = editor.getDocument().getLength() - 1; } while (caretPosition < 0) { try { String s = editor.getDocument().getText(caretPosition, 1); if ("\n".equals(s)) { break; } caretPosition--; } catch (BadLocationException e) { throw new RuntimeException(e); } } return caretPosition; } }