Java JTextComponent Column getColumnNumber(JTextComponent editor, int pos)

Here you can find the source of getColumnNumber(JTextComponent editor, int pos)

Description

Gets the column number at given position of editor.

License

Apache License

Parameter

Parameter Description
editor a parameter
pos a parameter

Exception

Parameter Description

Return

the 0 based column number

Declaration

public static int getColumnNumber(JTextComponent editor, int pos) throws BadLocationException 

Method Source Code

//package com.java2s;
/*/*w  w  w.ja  v  a  2 s  . c om*/
 * Copyright 2008 Ayman Al-Sairafi ayman.alsairafi@gmail.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License 
 *       at http://www.apache.org/licenses/LICENSE-2.0 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.  
 */

import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.text.BadLocationException;

import javax.swing.text.JTextComponent;

public class Main {
    /**
     * Gets the column number at given position of editor.  The first column is
     * ZERO
     * @param editor
     * @param pos
     * @return the 0 based column number
     * @throws javax.swing.text.BadLocationException
     */
    public static int getColumnNumber(JTextComponent editor, int pos) throws BadLocationException {
        // speedup if the pos is 0
        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;
    }
}

Related

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