Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Fubarable

Thread: limit characters in textfield in swing

  1. #1
    heman is offline Member
    Join Date
    Aug 2012
    Posts
    39
    Rep Power
    0

    Default limit characters in textfield in swing

    I am designing a calculator in which user enters the numbers by clicking a mouse button.
    The problem is it takes endless characters and i want to limit the number of input characters to 10.
    I tried using "setColumns(10)" to textfield but it does not solve the problem .
    Can anyone show me the way to limit number of characters.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: limit characters in textfield in swing

    one way is to use a FormattedTextField. Myself I kind of like using a DocumentFilter as it is quite flexible.

  3. #3
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: limit characters in textfield in swing

    Use Document Listener to your text field.. then override "public void insertUpdate(DocumentEvent E)" method..
    in that You will Place Your Condition as textField.getDocument().getLength() is not more than 10.. if entered more than ten U will display any Message Box.. I think it will work for You.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: limit characters in textfield in swing

    Quote Originally Posted by ch.nagaraju111 View Post
    Use Document Listener to your text field.. then override "public void insertUpdate(DocumentEvent E)" method..
    in that You will Place Your Condition as textField.getDocument().getLength() is not more than 10.. if entered more than ten U will display any Message Box.. I think it will work for You.
    No, this is the wrong way to go as it checks the text after it has been added to the textcompoment. Again, much better is to use a DocumentFilter which checks the text before it is added to the component, and so you can delete it, improve it, or otherwise modify it before adding it to the component. The filter is definitely the way to go.
    DarrylBurke likes this.

  5. #5
    heman is offline Member
    Join Date
    Aug 2012
    Posts
    39
    Rep Power
    0

    Default Re: limit characters in textfield in swing

    Quote Originally Posted by Fubarable View Post
    No, this is the wrong way to go as it checks the text after it has been added to the textcompoment. Again, much better is to use a DocumentFilter which checks the text before it is added to the component, and so you can delete it, improve it, or otherwise modify it before adding it to the component. The filter is definitely the way to go.
    thanks.

Similar Threads

  1. Replies: 1
    Last Post: 04-30-2012, 05:28 AM
  2. How to limit characters textbox in java
    By rjagan in forum New To Java
    Replies: 11
    Last Post: 03-28-2011, 11:09 PM
  3. How to make Swing JTextField look like AWT TextField?
    By LeonLanford in forum AWT / Swing
    Replies: 1
    Last Post: 09-04-2010, 05:06 PM
  4. Replies: 6
    Last Post: 03-06-2009, 10:51 PM
  5. Displaying Chinese characters on Swing components
    By vaskarbasak in forum AWT / Swing
    Replies: 3
    Last Post: 06-26-2008, 09:27 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
  •