Re: JTextArea contraints
To: comp.lang.java.gui
jc_halsey@yahoo.com wrote:
Could you explain what you mean by line feeds?
Knute Johnson wrote:
jc_halsey@yahoo.com wrote:
I have tried and cont. to limit the rows(lines) on a JTextArea.
I have created a PlainDoc. and have successfully limited the max
char(s).
The max chars is good for the text area as a whole if you start at 0
and fill every line.
However in this case, which would be a text editor, a vertical limit is
needed.
The user can not exceed past row/line 17. As is now, the user can type
past line 17.
Any suggestions on how to limit?
Posted is the Plain Doc., to show the limit on maxchar(s)
public class MaxLengthDocument extends PlainDocument {
private int maxRows = 0;
// create a Document with a specified max length
public MaxLengthDocument(int maxLength) {
max = maxLength;
}
// don't allow an insertion to exceed the max length
public void insertString(int offset, String str, AttributeSet a)
throws BadLocationException {
if (getLength() + str.length() > max )
java.awt.Toolkit.getDefaultToolkit().beep();
else
super.insertString(offset, str, a);
}
}
I think I would try to count line feeds in your Document. If there are
already 16 don't allow any text to be entered that contains a line feed.
--
Knute Johnson
email s/nospam/knute/
You said that you wanted to limit the number of lines in a JTextArea.
The only way you can have another line is to have a linefeed in the
text. That is unless you are using word wrap in which case you probably
can never tell where the 17th line will be.
So in your Document, count the linefeeds and limit them to 16 or whatever.
Or is the question that you don't know what a linefeed is?
Linefeed is the character that causes the display to move to the next
line. It is usually a hex 0A or \n character.
--
Knute Johnson
email s/nospam/knute/
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24