Java JTextComponent Column getColumnAtPosition(JTextComponent editor, int caretPosition)

Here you can find the source of getColumnAtPosition(JTextComponent editor, int caretPosition)

Description

get Column At Position

License

Apache License

Declaration

public static int getColumnAtPosition(JTextComponent editor, int caretPosition) 

Method Source Code

//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;
    }
}

Related

  1. getColumn(int pos, JTextComponent editor)
  2. getColumn(JTextComponent editor, int pos)
  3. getColumnNumber(JTextComponent editor, int pos)
  4. getDocumentPosition(JTextComponent editor, int line, int column)
  5. setCaretPosition(JTextComponent target, int line, int column)