title: How to limit JTextArea max Rows and Columns?
body: <p>I am using JTextArea in JScrollPane</p>
  
  <p>I want to limit the maximum number of lines possible and the maximum chars in each line.</p>
  
  <p>I need that the string will be exactly like on the screen, each line will end with '\n' (if there another line after it) and the user will be able to insert only X lines and Y chars in each line.</p>
  
  <p>I tried to limit the lines but I don't know exactly how many lines do I have because of the line wrapping, The line wrapping is starting new line visually on the screen(because of the width of the JTextArea) but in the string of the component it is really the same line with no '\n' to indicate new line. I do not have an idea how to limit the max chars in each line while typing.</p>
  
  <p>There are 2 stages:</p>
  
  <ol>
  <li>The typing of the string- keep that the user will not be able to type more then X lines and Y chars in each line. (even if the line wrap only visualy or the user typed '/n')</li>
  <li>Insert the string to the DB- after cliking 'OK' convert the string that every line will end with "/n" even if the user did not typed it and the line was wrapped only visualy.</li>
  </ol>
  
  <p>There are few problems if i will count the chars in the line and insert '/n' in the end of the line, thats why i decided to do it in two stages. In the first stage ehile the user is typing i would rather only limit it visualy and force line wrpping or something similar. Only in the second stage when i save string i will add the '/n' even if the user did not typed it in the end of the lines!</p>
  
  <p><strong>Does anyone have an idea?</strong></p>
  
  <hr>
  
  <p>I know that i will have to use DocumentFilter OR StyledDocument.</p>
  
  <p>Here is sample code that limit only the lines to 3:(but not the chars in row to 19) </p>
  
  <pre><code>private JTextArea textArea ;
  textArea  = new JTextArea(3,19);
  textArea  .setLineWrap(true);
  textArea .setDocument(new LimitedStyledDocument(3));
  JScrollPane scrollPane  = new JScrollPane(textArea
  
  public class LimitedStyledDocument extends DefaultStyledDocument
  
      /** Field maxCharacters */
      int maxLines;
  
      public LimitedStyledDocument(int maxLines) {
          maxCharacters = maxLines;
      }
  
  public void insertString(int offs, String str, AttributeSet attribute) throws BadLocationException {
      Element root = this.getDefaultRootElement();
      int lineCount = getLineCount(str);
  
      if (lineCount + root.getElementCount() &lt;= maxLines){
          super.insertString(offs, str, attribute);
      }
      else {
          Toolkit.getDefaultToolkit().beep();
      }
  }
  
   /**
   * get Line Count
   * 
   * @param str
   * @return the count of '\n' in the String
   */
  private int getLineCount(String str){
      String tempStr = new String(str);
      int index;
      int lineCount = 0;
  
      while (tempStr.length() &gt; 0){
          index = tempStr.indexOf("\n");
          if(index != -1){
          lineCount++;
              tempStr = tempStr.substring(index+1);
          }
      else{
          break;
          }
      }
      return lineCount;
     }
  }
  </code></pre>