How to limit rows and columns of JTextArea max?

I am using JTextArea in JScrollPane

I want to limit the maximum number of possible lines and the maximum characters in each line.

I need the line to be exactly the same as on the screen, each line ends with "\ n" (if there is another line after it), and the user can insert only X lines and Y characters in each line.

I tried to limit the lines, but I do not know exactly how many lines I have due to line breaks. Linear packaging starts visually visually on the screen (due to the width of the JTextArea), but the line of the component is really the same line for which '\ n' is not specified to indicate a new line. I do not know how to limit the maximum characters in each line as I type.

There are 2 steps:

  • Having typed a line, make sure that the user cannot type more lines X and Y in each line. (even if the line break is only visual or the user typed "/ n")
  • Insert a line in DB - after clicking “OK”, it converts a line in which each line ends with “/ n”, even if the user did not type it and the line was wrapped only visually.

There are several problems if I count the characters in a line and insert '/ n' at the end of the line, which is why I decided to do this in two steps. At the first stage, the user prints the ehile, I would prefer to limit it to visual and force wrpping lines or something like that. Only in the second stage, when I save the line, will I add '/ n', even if the user did not type it at the end of the lines!

Anyone have an idea?


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

Here is an example code that restricts only strings to 3: (but not characters in a string 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; } } 
+4
source share
2 answers

The following worked for me:

 public class LimitedLinesDocument extends DefaultStyledDocument { private static final String EOL = "\n"; private int maxLines; public LimitedLinesDocument(int maxLines) { this.maxLines = maxLines; } public void insertString(int offs, String str, AttributeSet attribute) throws BadLocationException { if (!EOL.equals(str) || StringUtils.occurs(getText(0, getLength()), EOL) < maxLines - 1) { super.insertString(offs, str, attribute); } } } 

If the StringUtils.occurs method looks like this:

 public static int occurs(String str, String subStr) { int occurrences = 0; int fromIndex = 0; while (fromIndex > -1) { fromIndex = str.indexOf(subStr, occurrences == 0 ? fromIndex : fromIndex + subStr.length()); if (fromIndex > -1) { occurrences++; } } return occurrences; } 
+1
source

Here is my version based on the Nick Holt version.

For records only, this will be used in the prototype and not much has been tested (if at all).

 public class LimitedLinesDocument extends DefaultStyledDocument { private static final long serialVersionUID = 1L; private static final String EOL = "\n"; private final int maxLines; private final int maxChars; public LimitedLinesDocument(int maxLines, int maxChars) { this.maxLines = maxLines; this.maxChars = maxChars; } @Override public void insertString(int offs, String str, AttributeSet attribute) throws BadLocationException { boolean ok = true; String currentText = getText(0, getLength()); // check max lines if (str.contains(EOL)) { if (occurs(currentText, EOL) >= maxLines - 1) { ok = false; } } else { // check max chars String[] lines = currentText.split("\n"); int lineBeginPos = 0; for (int lineNum = 0; lineNum < lines.length; lineNum++) { int lineLength = lines[lineNum].length(); int lineEndPos = lineBeginPos + lineLength; System.out.println(lineBeginPos + " " + lineEndPos + " " + lineLength + " " + offs); if (lineBeginPos <= offs && offs <= lineEndPos) { System.out.println("Found line"); if (lineLength + 1 > maxChars) { ok = false; break; } } lineBeginPos = lineEndPos; lineBeginPos++; // for \n } } if (ok) super.insertString(offs, str, attribute); } public int occurs(String str, String subStr) { int occurrences = 0; int fromIndex = 0; while (fromIndex > -1) { fromIndex = str.indexOf(subStr, occurrences == 0 ? fromIndex : fromIndex + subStr.length()); if (fromIndex > -1) { occurrences++; } } return occurrences; } } 
+1
source

All Articles