How to limit JTextArea max Rows and Coloums?

Posted by Billbo bug on Stack Overflow See other posts from Stack Overflow or by Billbo bug
Published on 2009-01-26T09:05:23Z Indexed on 2010/03/18 3:11 UTC
Read the original article Hit count: 437

Filed under:
|
|
|
|

I am using JTextArea in JScrollPane I want to limit the maximum number of lines possible and the maximum chars in each line. 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.

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 visualy 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.

There are 2 stages:

  1. 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')
  2. 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.

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!

Does anyone have an idea?


I know that i will have to use DocumentFilter OR StyledDocument.

Here is sample code that limit only the lines to 3:(but not the chars in row to 19)

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() <= 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() > 0){
    	index = tempStr.indexOf("\n");
    	if(index != -1){
		lineCount++;
    		tempStr = tempStr.substring(index+1);
    	}
	else{
		break;
    	}
    }
    return lineCount;
   }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about jtextarea