Java JTextComponent makeVisible(JTextComponent textPane, int start, int end)

Here you can find the source of makeVisible(JTextComponent textPane, int start, int end)

Description

Scroll a text component inside a viewport such that the text at the specified position becomes visible.

License

Open Source License

Parameter

Parameter Description
textPane The text pane which is scrolled.
start Start offset in the document.
end End offset in the document.

Declaration

public static void makeVisible(JTextComponent textPane, int start, int end) 

Method Source Code

//package com.java2s;
/*//from ww  w.  j  av a 2s.c om
 * Copyright (C) 2001-2013 Michael Koch (tensberg@gmx.net)
 *
 * This file is part of JGloss.
 *
 * JGloss is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * JGloss is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JGloss; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 */

import java.awt.Rectangle;

import javax.swing.text.JTextComponent;

public class Main {
    /**
     * Scroll a text component inside a viewport such that the text at the specified position
     * becomes visible. If the text is already fully visible, no scrolling is done.
     *
     * @param textPane The text pane which is scrolled.
     * @param start Start offset in the document.
     * @param end End offset in the document.
     */
    public static void makeVisible(JTextComponent textPane, int start, int end) {
        try {
            Rectangle r1 = textPane.modelToView(start);
            // end-1 selects the last character of the annotation element
            Rectangle r2 = textPane.modelToView(end - 1);
            if (r1 != null) {
                if (r2 != null) {
                    r1 = r1.createUnion(r2).getBounds();
                }

                if (!textPane.getVisibleRect().contains(r1)) {
                    textPane.scrollRectToVisible(r1);
                }
            }
        } catch (javax.swing.text.BadLocationException ex) {
            // can happen if layout of text component is not done yet
        }
    }
}

Related

  1. isEmpty(JTextComponent component)
  2. isJavaContext(final JTextComponent component, final int offset, final boolean allowInStrings)
  3. isNonWhitespaceBetween(JTextComponent editor, int iStart, int iEnd)
  4. loadFileToPane(String fname, JTextComponent pane)
  5. loadTextToPane(String text, JTextComponent pane, boolean append)
  6. maxLength(JTextComponent textComponent, int length)
  7. paintCurrentLineBackground(final Graphics g, final JTextComponent c, final Color col)
  8. paintUnderline(Graphics g, JTextComponent tc, int pos0, int pos1)
  9. parseDouble(JTextComponent textComponent)