Given an offset, returns the column number in a text component. - Java 2D Graphics

Java examples for 2D Graphics:Text

Description

Given an offset, returns the column number in a text component.

Demo Code


//package com.java2s;

import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.JTextComponent;

public class Main {
    /**/*from  w w  w  .ja  v a 2  s.  c o  m*/
     * Given an offset, returns the column number in a text component.
     * 
     * @param offset Offset position
     * @return Column number starting in 0.
     */
    public static int getCaretColumn(JTextComponent txtCmp) {
        return getColumn(txtCmp, txtCmp.getCaretPosition());

    }

    /**
     * Given an offset, returns the column number in a text component.
     * 
     * @param offset Offset position
     * @return Column number starting in 0.
     */
    public static int getColumn(JTextComponent txtCmp, int offset) {
        return txtCmp.getCaretPosition()
                - getOffsetStartLine(txtCmp, offset);
    }

    /**
     * Given an offset, returns the offset where the line begins.
     * 
     * @param offset Offset position.
     * @return Offset where the line begins.
     */
    public static int getOffsetStartLine(JTextComponent txtCmp, int offset) {
        return getOffsetStartLine(txtCmp.getDocument(), offset);
    }

    /**
     * Given an offset, returns the offset where the line begins.
     * 
     * @param doc JTextComponent document
     * @param offset Offset position.
     * @return Offset where the line begins.
     */
    public static int getOffsetStartLine(Document doc, int offset) {
        int line = getLine(doc, offset);
        return getWhereLineStarts(doc, line);
    }

    /**
     * Given an offset, returns the line number of a text component.
     * 
     * @param txtCmp Text Component
     * @param offset Offset position
     * @return Line number starting in 0.
     */
    public static int getLine(JTextComponent txtCmp, int offset) {
        return getLine(txtCmp.getDocument(), offset);
    }

    /**
     * Given an offset, returns the line number in a text component.
     * 
     * @param doc JTextComponent document
     * @param offset Offset position
     * @return Line number starting in 0.
     */
    public static int getLine(Document doc, int offset) {
        Element root = doc.getDefaultRootElement();
        return root.getElementIndex(offset);
    }

    /**
     * Given a line returns the offset number where the line starts.
     * 
     * @param line Line number
     * @return Offset number
     */
    public static int getWhereLineStarts(JTextComponent txtCmp, int line) {
        return getWhereLineStarts(txtCmp.getDocument(), line);
    }

    /**
     * Given a line returns the offset number where the line starts.
     * 
     * @param doc JTextComponent document
     * @param line Line number
     * @return Offset number
     */
    public static int getWhereLineStarts(Document doc, int line) {
        Element el = doc.getDefaultRootElement().getElement(line);
        if (el != null)
            return el.getStartOffset();

        return doc.getStartPosition().getOffset();
    }
}

Related Tutorials