Count the line number from JTextComponent. - Java 2D Graphics

Java examples for 2D Graphics:Text

Description

Count the line number from JTextComponent.

Demo Code


//package com.java2s;

import javax.swing.text.Document;

import javax.swing.text.JTextComponent;

public class Main {
    /**//from ww w  .  ja  v  a2  s . c  o m
     * Count the line number.
     * 
     * @return Amount of lines.
     */
    public static int countLines(JTextComponent txtCmp) {
        return countLines(txtCmp.getDocument());
    }

    /**
     * Count the line number.
     * 
     * @param doc JTextComponent document
     * @return Amount of lines.
     */
    public static int countLines(Document doc) {
        return doc.getDefaultRootElement().getElementCount();
    }
}

Related Tutorials