Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Urgent code needed

  1. #1
    Member
    Join Date
    Aug 2009
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Urgent code needed

    How to limit the input length of JTextArea using events?Is it possible?


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Urgent code needed

    Have a look at the addInputMethodListener method to see if you can add an input listener and every time an event comes in you will have to check the text in the textarea and cut it if its too long.

    JTextComponent (Java Platform SE 6))

    // Json

  3. #3
    Member
    Join Date
    Aug 2009
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Re: Urgent code needed

    I tried with InputMethodTextChanged() action.But it didn't provide any feasible solution.Also with

    private void tfBannerKeyTyped(java.awt.event.KeyEvent evt) {
    if(tfBanner.getText().length() > 10)
    evt.consume();
    }
    here, we couldn't enter any text.But can to copy and paste.I don't want to do that.Please provide some other possible solution please.

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Urgent code needed

    I'm not sure how you create your JTextArea, but after some googling this is what I found.

    http://java.sun.com/docs/books/tutor...izeFilter.java

    You need to do something like this.

            final PlainDocument plainDocument = new PlainDocument();
            plainDocument.setDocumentFilter(new DocumentSizeFilter(10));
     
            final JTextArea textArea = new JTextArea(plainDocument);

    Add this class where appropriate.

        private static class DocumentSizeFilter extends DocumentFilter {
            int maxCharacters;
     
            public DocumentSizeFilter(int maxChars) {
                maxCharacters = maxChars;
            }
     
            public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException {
                    System.out.println("in DocumentSizeFilter's insertString method");
     
                if ((fb.getDocument().getLength() + str.length()) <= maxCharacters)
                    super.insertString(fb, offs, str, a);
                else
                    Toolkit.getDefaultToolkit().beep();
            }
     
            public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a) throws BadLocationException {
                    System.out.println("in DocumentSizeFilter's replace method");
     
                if ((fb.getDocument().getLength() + str.length() - length) <= maxCharacters)
                    super.replace(fb, offs, length, str, a);
                else
                    Toolkit.getDefaultToolkit().beep();
            }
     
        }

    // Json

  5. #5
    Member
    Join Date
    Aug 2009
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [SOLVED]Re: Urgent code needed

    Thanks Json.Its working.

    final PlainDocument plainDocument = new PlainDocument();
    plainDocument.setDocumentFilter(new DocumentSizeFilter(10));

    final JTextArea textArea = new JTextArea(plainDocument); - Instead of this i use the coding,

    PlainDocument pDoc=(PlainDocument)tfBanner.getDocument();
    pDoc.setDocumentFilter(new DocumentSizeFilter(10));

    Its working exactly what i want.Thanks a lot.

Similar Threads

  1. HELP "code needed"
    By Dave in forum File I/O & Other I/O Streams
    Replies: 14
    Last Post: August 26th, 2009, 11:06 AM
  2. Replies: 2
    Last Post: May 16th, 2009, 05:23 AM
  3. Replies: 1
    Last Post: May 4th, 2009, 06:30 AM
  4. Replies: 4
    Last Post: May 22nd, 2008, 10:59 AM