Java JTextComponent getPositionBelow(JTextComponent c, int offs, int x)

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

Description

Determines the position in the model that is closest to the given view location in the row below.

License

Open Source License

Parameter

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

Return

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

Declaration

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

Method Source Code


//package com.java2s;
/*/*from   ww w.j  av  a  2 s  . co  m*/
 * @(#)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 position in the model that is closest to the given 
     * view location in the row below.  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
     * @param x the X coordinate >= 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 getPositionBelow(JTextComponent c, int offs, int x) throws BadLocationException {
        int lastOffs = getRowEnd(c, offs) + 1;
        if (lastOffs <= 0) {
            return -1;
        }
        int bestSpan = Short.MAX_VALUE;
        int n = c.getDocument().getLength();
        int y = 0;
        Rectangle r = null;
        if (lastOffs <= n) {
            r = c.modelToView(lastOffs);
            y = r.y;
        }
        while ((r != null) && (y == r.y)) {
            int span = Math.abs(x - r.x);
            if (span < bestSpan) {
                offs = lastOffs;
                bestSpan = span;
            }
            lastOffs += 1;
            r = (lastOffs <= n) ? c.modelToView(lastOffs) : null;
        }
        return offs;
    }

    /**
     * Determines the ending 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 getRowEnd(JTextComponent c, int offs) throws BadLocationException {
        Rectangle r = c.modelToView(offs);
        if (r == null) {
            return -1;
        }
        int n = c.getDocument().getLength();
        int lastOffs = offs;
        int y = r.y;
        while ((r != null) && (y == r.y)) {
            offs = lastOffs;
            lastOffs += 1;
            r = (lastOffs <= n) ? c.modelToView(lastOffs) : null;
        }
        return offs;
    }
}

Related

  1. fillWith(String value, JTextComponent... fields)
  2. getAdjustedClickCount(JTextComponent comp, MouseEvent e)
  3. getCurrentTextBlock(JTextComponent textComponent)
  4. getNumberOfLines(final JTextComponent component)
  5. getParagraphElement(JTextComponent c, int offs)
  6. getPreferredScrollableViewportSize(javax.swing.text.JTextComponent t, Dimension ans)
  7. getRenderedLineOfChar(JTextComponent comp, int charIdx)
  8. getStoredBackground(JTextComponent comp)
  9. getStoredBackground(JTextComponent comp)