Here you can find the source of getPositionBelow(JTextComponent c, int offs, int x)
Parameter | Description |
---|---|
c | the editor |
offs | the offset in the document >= 0 |
x | the X coordinate >= 0 |
public static final int getPositionBelow(JTextComponent c, int offs, int x) throws BadLocationException
//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; } }