Java JTextComponent Row getRowStart(JTextComponent c, int offs)

Here you can find the source of getRowStart(JTextComponent c, int offs)

Description

Determines the starting row model position of the row that contains the specified model position.

License

Open Source License

Parameter

Parameter Description
c the editor
offs the offset in the document >= 0

Return

the position >= 0 if the request can be computed, otherwise a value of -1 will be returned.

Declaration

public static final int getRowStart(JTextComponent c, int offs) throws BadLocationException 

Method Source Code


//package com.java2s;
/*/*from   ww w .j  a v  a2  s  .  c om*/
 * @(#)Utilities.java   1.38 01/12/03
 *
 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import javax.swing.text.*;

import java.awt.Rectangle;

public class Main {
    /**
     * Determines the starting row model position of the row that contains
     * the specified model position.  The component given must have a
     * size to compute the result.  If the component doesn't have a size
     * a value of -1 will be returned.
     *
     * @param c the editor
     * @param offs the offset in the document >= 0
     * @return the position >= 0 if the request can be computed, otherwise
     *  a value of -1 will be returned.
     * @exception BadLocationException if the offset is out of range
     */
    public static final int getRowStart(JTextComponent c, int offs) throws BadLocationException {
        Rectangle r = c.modelToView(offs);
        if (r == null) {
            return -1;
        }
        int lastOffs = offs;
        int y = r.y;
        while ((r != null) && (y == r.y)) {
            offs = lastOffs;
            lastOffs -= 1;
            r = (lastOffs >= 0) ? c.modelToView(lastOffs) : null;
        }
        return offs;
    }
}

Related

  1. getRow(int pos, JTextComponent editor)
  2. getRow(JTextComponent editor, int pos)