Get last visible line of the text component in a JScrollPane. - Java 2D Graphics

Java examples for 2D Graphics:Text

Description

Get last visible line of the text component in a JScrollPane.

Demo Code


//package com.java2s;
import java.awt.FontMetrics;

import javax.swing.text.JTextComponent;
import javax.swing.text.View;

public class Main {
    /**// w w w .  j a va 2 s . c  o m
     * Get last visible line of the text component in a JScrollPane.
     * 
     * @param txtCmp Text component.
     * @return The line.
     */
    public static int getBottomLine(JTextComponent txtCmp) {
        FontMetrics fm = txtCmp.getFontMetrics(txtCmp.getFont());
        int fontHeight = fm.getHeight();

        View view = txtCmp.getUI().getRootView(txtCmp).getView(0);
        int height = view.getContainer().getParent().getHeight();

        return (Math.abs(txtCmp.getY()) + fm.getAscent() + height)
                / fontHeight;
    }
}

Related Tutorials