Results 1 to 7 of 7
- 06-27-2010, 01:36 PM #1
Limit number of cols in JTextArea
Hi all,
I'd like to limit the number of columns in a JTextArea to 80characters (per line). I've been searching on google but no result.
So far, I've been extending a PlainDocument and overriding the insertString method. I've also got it to limit the first line to 80 characters.
You can start a new line (the second row), but you cannot type any characters at all. If you try to go to the third line, an ArrayIndexOutOfBoundsException occurs.
Thanks for reading,
Berkeleybross
Java Code:private class myContentDocument extends PlainDocument { @Override public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { String[] lines = getText( 0, getLength()).split("\r\n"); boolean valid = true; int line = myContentArea.getLineOfOffset (offs); if (((lines[line].length()) + str.length()) <= 80 ) { super.insertString(offs, str, a); } else { Toolkit.getDefaultToolkit().beep(); } } }
The following exception is at the line "if (((lines[line].length()) + str.length()) <= 80 ) {"
Im presuming lines[1] is out of range, but that shouldnt be the case because I split on the line break and the caret is on the second line.
Java Code:Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 at blogger.MainPanel$myContentDocument.insertString(MainPanel.java:490) at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:655) at javax.swing.text.JTextComponent.replaceSelection(JTextComponent.java:1351) at javax.swing.text.DefaultEditorKit$InsertBreakAction.actionPerformed(DefaultEditorKit.java:952) at javax.swing.plaf.basic.BasicTextUI$TextActionWrapper.actionPerformed(BasicTextUI.java:2066) at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1633) at javax.swing.JComponent.processKeyBinding(JComponent.java:2839) at javax.swing.JComponent.processKeyBindings(JComponent.java:2874) at javax.swing.JComponent.processKeyEvent(JComponent.java:2802) at java.awt.Component.processEvent(Component.java:6040) at java.awt.Container.processEvent(Container.java:2041) at java.awt.Component.dispatchEventImpl(Component.java:4630) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1850) at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:712) at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:990) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:855) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:676) at java.awt.Component.dispatchEventImpl(Component.java:4502) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Window.dispatchEventImpl(Window.java:2475) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
- 06-27-2010, 01:42 PM #2
Can you try debugging the code by using println() to show the values returned by getText() and what is in the lines[] array?Im presuming lines[1] is out of range
- 06-27-2010, 01:43 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Would letting the JTextArea do it itself help? See the API documentation for the setWrapStyleWord( ... ) and setLineWrap( ... ) methods.
kind regards,
Jos
- 06-27-2010, 02:00 PM #4
Thanks for replying so quickly!
@Norm: the line variable is set by the textArea.getLineOfOffset() method, which is returning the correct line. GetText() returns the right text, but lines[] only has one item in it. Not sure why, as getText() definatly shows \r\n in it so it should be making a second item.
@JosAH I tried using
It wraps perfectly, but not necessarily at the 80th column - it wraps when it reaches the end of its size (which swing helpfully resizes for me).Java Code:myContentArea.setWrapStyleWord(true); myContentArea.setLineWrap(true); myContentArea.setColumns(80);
Also, i want to manually insert \r\n in at the appropriate places as i want to put it into a database with a max col of 80.
Thanks,
Berkeleybross
- 06-27-2010, 02:16 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 06-27-2010, 02:19 PM #6
ahem. Yeah I could do that.
Hadnt thought of it that way :P
Thanks!
Berkeleybross
- 06-27-2010, 03:20 PM #7
Similar Threads
-
jtextfield limit?
By devstarter in forum New To JavaReplies: 1Last Post: 03-01-2010, 06:17 PM -
JTextArea on PopUp -JTextArea isn't editable
By Richy76 in forum AWT / SwingReplies: 3Last Post: 02-01-2010, 07:51 PM -
Web services packet limit?
By poet in forum Advanced JavaReplies: 1Last Post: 10-21-2009, 01:57 AM -
Can you Limit JSpinners??
By ashton in forum New To JavaReplies: 1Last Post: 02-09-2009, 06:10 AM -
Limit of String
By javaplus in forum New To JavaReplies: 5Last Post: 11-19-2007, 04:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks