Get Line Count Of JTextArea Including Word Wrap Lines

After almost a work day’s worth of searching, I was able to find a solution to my dilemna. While JTextArea has a getLineCount() method to return the number of lines, I wanted a way to also detect wrapped lines since I wanted to set my component a fixed height if the maximum height limit is reached.

This method by user Jörg in the Java forum did the trick.

Related Posts Plugin for WordPress, Blogger...

6 comments

  1. Very helpful. Thank you.
    P.S.: if used in for example a CaretListener ensure to run it via SwingUtils.invokeLater otherwise you may get wrong (more precisely a 0) as a result.

    1. @smurffy: yes, you are right. If there is something wrong that does not work here, ensure that any actions is in the SwingUtilities.invokeLater() method

Leave a Reply

Your email address will not be published. Required fields are marked *