Java Utililty Methods JTextComponent Column

List of utility methods to do JTextComponent Column

Description

The list of methods to do JTextComponent Column are organized into topic(s).

Method

intgetColumn(int pos, JTextComponent editor)
get Column
return pos - Utilities.getRowStart(editor, pos) + 1;
intgetColumn(JTextComponent editor, int pos)
get Column
try {
    return pos - Utilities.getRowStart(editor, pos) + 1;
} catch (BadLocationException e) {
    e.printStackTrace();
return -1;
intgetColumnAtPosition(JTextComponent editor, int caretPosition)
get Column At Position
if (editor.getDocument().getLength() == 0) {
    return 0;
if (caretPosition >= editor.getDocument().getLength() - 1) {
    caretPosition = editor.getDocument().getLength() - 1;
while (caretPosition < 0) {
    try {
...
intgetColumnNumber(JTextComponent editor, int pos)
Gets the column number at given position of editor.
if (pos == 0) {
    return 0;
Rectangle r = editor.modelToView(pos);
int start = editor.viewToModel(new Point(0, r.y));
int column = pos - start;
return column;
intgetDocumentPosition(JTextComponent editor, int line, int column)
Get the closest position within the document of the component that has given line and column.
int lineHeight = editor.getFontMetrics(editor.getFont()).getHeight();
int charWidth = editor.getFontMetrics(editor.getFont()).charWidth('m');
int y = line * lineHeight;
int x = column * charWidth;
Point pt = new Point(x, y);
int pos = editor.viewToModel(pt);
return pos;
voidsetCaretPosition(JTextComponent target, int line, int column)
Sets the caret position of the given target to the given line and column
int p = getDocumentPosition(target, line, column);
target.setCaretPosition(p);