title: How to count the number of lines in a JTextArea, including those caused by wrapping?
body: <p>I have a JTextArea for which I have set word-wrap and wrap-style-word to true. I want to "pack" the JTextArea to the minimum possible height given a specified width.</p>
  
  <p>To do this, I'm planning calculating the height of the font using...</p>
  
  <pre><code>  Font font = jTextArea.getFont();
    FontMetrics fontMetrics = jTextArea.getFontMetrics(font);
    int lineHeight = fontMetrics.getAscent() + fontMetrics.getDescent();
  </code></pre>
  
  <p>...and then multiply this by the number of lines used in the JTextArea. The problem is that JTextArea.getLineCount() counts the number of line returns ignoring the wrapped lines.</p>
  
  <p>How do I count the number of lines used in a JTextArea including those that are caused by word wrap?</p>
  
  <p>Here's some demo code to make toying with this problem easier. I have a listener that prints out the number of lines each time the window is resized. At the moment, it always prints 1, but I want to to compensate for the word wrap and print out how many lines are actually being used.</p>
  
  <p><strong>EDIT: I've included the solution to the problem in the code below. The static countLines method gives the solution.</strong></p>
  
  <pre><code>package components;                                                                           
  
  import java.awt.*;
  import java.awt.event.*;
  import java.awt.font.*;
  import java.text.*;
  import javax.swing.*;
  
  public class JTextAreaLineCountDemo extends JPanel {                                          
    JTextArea textArea;                                                                         
  
    public JTextAreaLineCountDemo() {                                                           
      super(new GridBagLayout());                                                               
  
      String inputStr = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmo";
      textArea = new JTextArea(inputStr);                                                       
      textArea.setEditable(false);                                                              
      textArea.setLineWrap(true);                                                               
      textArea.setWrapStyleWord(true);                                                          
  
      // Add Components to this panel.                                                          
      GridBagConstraints c = new GridBagConstraints();                                          
      c.gridwidth = GridBagConstraints.REMAINDER;                                               
  
      c.fill = GridBagConstraints.BOTH;                                                         
      c.weightx = 1.0;                                                                          
      c.weighty = 1.0;                                                                          
      add(textArea, c);                                                                         
  
      addComponentListener(new ComponentAdapter() {                                             
        @Override                                                                               
        public void componentResized(ComponentEvent ce) {                 
          System.out.println("Line count: " + countLines(textArea));                         
        }                                                                                       
      });                                                                                       
    }                                                                                           
  
    private static int countLines(JTextArea textArea) {
      AttributedString text = new AttributedString(textArea.getText());
      FontRenderContext frc = textArea.getFontMetrics(textArea.getFont())
          .getFontRenderContext();
      AttributedCharacterIterator charIt = text.getIterator();
      LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(charIt, frc);
      float formatWidth = (float) textArea.getSize().width;
      lineMeasurer.setPosition(charIt.getBeginIndex());
  
      int noLines = 0;
      while (lineMeasurer.getPosition() &lt; charIt.getEndIndex()) {
        lineMeasurer.nextLayout(formatWidth);
        noLines++;
      }
  
      return noLines;
    }
  
    private static void createAndShowGUI() {                                                    
      JFrame frame = new JFrame("JTextAreaLineCountDemo");                                      
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                                     
  
      frame.add(new JTextAreaLineCountDemo());                                                  
  
      frame.pack();                                                                             
      frame.setVisible(true);                                                                   
    }                                                                                           
  
    public static void main(String[] args) {                                                    
      javax.swing.SwingUtilities.invokeLater(new Runnable() {                                   
        public void run() {                                                                     
          createAndShowGUI();                                                                   
        }                                                                                       
      });                                                                                       
    }                                                                                           
  }                                                                                             
  </code></pre>