Results 1 to 7 of 7
  1. #1
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default 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)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Im presuming lines[1] is out of range
    Can you try debugging the code by using println() to show the values returned by getText() and what is in the lines[] array?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default

    Would letting the JTextArea do it itself help? See the API documentation for the setWrapStyleWord( ... ) and setLineWrap( ... ) methods.

    kind regards,

    Jos

  4. #4
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    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
    Java Code:
    myContentArea.setWrapStyleWord(true);
    myContentArea.setLineWrap(true);
    myContentArea.setColumns(80);
    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).
    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

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by berkeleybross View Post
    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).
    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.
    Why don't you insert those end of line markers just before you write the text to the database? When you read it in later the JTextArea knows how to handle them ...

    kind regards,

    Jos

  6. #6
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    ahem. Yeah I could do that.

    Hadnt thought of it that way :P

    Thanks!

    Berkeleybross

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    GetText() returns the right text, but lines[] only has one item in it.
    Can you show the "right text" that the split returns one item for vs the number you expect?

Similar Threads

  1. jtextfield limit?
    By devstarter in forum New To Java
    Replies: 1
    Last Post: 03-01-2010, 06:17 PM
  2. JTextArea on PopUp -JTextArea isn't editable
    By Richy76 in forum AWT / Swing
    Replies: 3
    Last Post: 02-01-2010, 07:51 PM
  3. Web services packet limit?
    By poet in forum Advanced Java
    Replies: 1
    Last Post: 10-21-2009, 01:57 AM
  4. Can you Limit JSpinners??
    By ashton in forum New To Java
    Replies: 1
    Last Post: 02-09-2009, 06:10 AM
  5. Limit of String
    By javaplus in forum New To Java
    Replies: 5
    Last Post: 11-19-2007, 04:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •