That just might work, but i am using word wrap....
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/
Well you got me intrigued so take a look at this example, it limits the
JTextArea to three lines of text.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class test {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
class LDoc extends PlainDocument {
public void insertString(int offs, String str,
AttributeSet a) throws BadLocationException {
String text = getText(0,getLength());
int lines = 0;
for (int i=0; i<text.length(); i++)
if (text.charAt(i) == '\n')
++lines;
System.out.println(str.indexOf('\n'));
if (lines == 2 && str.indexOf('\n') >= 0)
return;
else
super.insertString(offs, str, a);
}
};
JTextArea ta = new JTextArea(new LDoc(),"",5,20);
f.add(ta,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}
--
Knute Johnson
email s/nospam/knute/
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!