Results 1 to 12 of 12
  1. #1
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Smile How to limit characters textbox in java

    Hi Friends,

    Am using netbeans for develop gui application.. i have placed one textbox(jTextField) from swing controls.. i want to enter only 4 letters in that textbox.. i.e., number or alphabet anything.. :)

    Any one please help me how to limit the text box values entering...


    Plzzzzzzzzzzzz Helppppppppp meeeeeeeee my dearrrrrrr friendssssssssss.......

  2. #2
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    For this you need to implement this class

    Java Code:
    import javax.swing.text.*;
    
    public class JTextFieldLimit extends PlainDocument {
        private int limit;
        // optional uppercase conversion
        private boolean toUppercase = false;
        
        JTextFieldLimit(int limit) {
            super();
            this.limit = limit;
        }
        
        JTextFieldLimit(int limit, boolean upper) {
            super();
            this.limit = limit;
            toUppercase = upper;
        }
        
        public void insertString
                (int offset, String  str, AttributeSet attr)
                throws BadLocationException {
            if (str == null) return;
            
            if ((getLength() + str.length()) <= limit) {
                if (toUppercase) str = str.toUpperCase();
                super.insertString(offset, str, attr);
            }
        }
    }
    then implement in your class like this

    Java Code:
        public static void main(String args[]) {
    		TestField mainForm = new TestField();
            textfield1 = new JTextField(15);
            mainForm.add(label1);
            mainForm.add(textfield1);
            textfield1.setDocument(new JTextFieldLimit(10)); // Change this value to 4
    		mainForm.setVisible(true);
        }
    let us know in case of any issue
    sanjeev,संजीव

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Plzzzzzzzzzzzz Helppppppppp meeeeeeeee my dearrrrrrr friendssssssssss.......
    Nooooo weeeee doooooonnn'ttt liiiiiike chiiiildiiiish poooooosts

    We also don't like people who start a thread and never bother to return to it.
    How to rename GUI Title

    db

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    For this you need to implement this class
    No need to extend anything. A simple DocumentFilter can do the trick.

    db

  5. #5
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Default

    Hi Friend..

    Thank u very much.. after test this code i will rep u ....

    Thank u .. thank u a lot

  6. #6
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Default

    Any other way to set the limit values in textbox properties

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Use a JFormattedTextField.

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

  9. #9
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Default

    Hai Darry.
    Your code is not working,. please send the correct code

  10. #10
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    Quote Originally Posted by rjagan View Post
    Hai Darry.
    Your code is not working,. please send the correct code
    Did you tried this?
    Java Code:
    JTextField jtextfield=new JTextField(5);
    Regards
    Ujjal

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Quote Originally Posted by rjagan View Post
    Hai Darry.
    Your code is not working
    Not surprising, since I didn't provide any.

    Quote Originally Posted by rjagan View Post
    please send the correct code
    Sure. Right after you finish washing my car.

    db

  12. #12
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    To implement a document filter, create a subclass of DocumentFilter and then attach it to a document using the setDocumentFilter method defined in the AbstractDocument class. Although it is possible to have documents that do not descend from AbstractDocument, by default Swing text components use AbstractDocument subclasses for their documents.
    The TextComponentDemo application has a document filter, DocumentSizeFilter, that limits the number of characters that the text pane can contain. Here is the code that creates the filter and attaches it to the text pane's document:

    Java Code:
    ...//Where member variables are declared:
    JTextPane textPane;
    AbstractDocument doc;
    static final int MAX_CHARACTERS = 300;
    ...
    textPane = new JTextPane();
    ...
    StyledDocument styledDoc = textPane.getStyledDocument();
    if (styledDoc instanceof AbstractDocument) {
        doc = (AbstractDocument)styledDoc;
        doc.setDocumentFilter(new DocumentSizeFilter(MAX_CHARACTERS));
    }

    you can find the tutorial for DocumentFilter here:
    Text Component Features (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

Similar Threads

  1. setting memory limit for java?
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 08-26-2010, 07:09 PM
  2. How to set value to a TextBox
    By kishan in forum JavaServer Faces (JSF)
    Replies: 3
    Last Post: 06-29-2010, 03:46 PM
  3. jtextfield limit?
    By devstarter in forum New To Java
    Replies: 1
    Last Post: 03-01-2010, 07:17 PM
  4. Can you Limit JSpinners??
    By ashton in forum New To Java
    Replies: 1
    Last Post: 02-09-2009, 07:10 AM
  5. Limit of String
    By javaplus in forum New To Java
    Replies: 5
    Last Post: 11-19-2007, 05: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
  •