Results 1 to 10 of 10
  1. #1
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default making a formatted text box that only takes 4 integers as input?

    Buddies i have to use a formatted text box that only takes integer values, also i want to fix the values to only 4 integers, neither less nor more than this.
    Please help me how can i do this...

    Thank you all.. in advance

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Use a DocumentFilter!

    Java Code:
     class DocumentSizeFilter extends DocumentFilter {
        private final int maxCharacters;
        private final String pattern;
        public DocumentSizeFilter(final int maxChars, final String pattern) {
            maxCharacters = maxChars;
            this.pattern = pattern;
        }
        @Override
        public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)
                throws BadLocationException {
            if (str.matches(pattern) && (fb.getDocument().getLength() + str.length() - length) <= maxCharacters) {
                super.replace(fb, offs, length, str, a);
            } else {
                Toolkit.getDefaultToolkit().beep(); // :D
            }
        }
    }
    Example to use:
    Java Code:
    ......
    		JTextField textfield = new JTextField(4);
    		((AbstractDocument) textfield.getDocument()).setDocumentFilter(new DocumentSizeFilter(4, "\\d+"));

  3. #3
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default

    Hey thankyou very much... it works very good...
    Since i m using NetBeans IDE the design i have created is by using swing pallete, now if i create the text field as said by you than how can i placed it onto the desired position??
    Is there a way to transform the textfield in my design with this one???

  4. #4
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default Yup got it!

    Thank you very much for your help, it finally works as i need it to be....
    I use the Customize Code property of the textfield and change the values there as given by you....

    Thanks again

  5. #5
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default pattern for text example: AB91

    Hey do me one more favour please, give me the value of pattern such that i can be pass it through the constructor of DocumentSizeFilter so that i can only input the values as:
    AB91
    GH78

    etc etc..........please help me
    and thank you

  6. #6
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    only two upper case letters and two digits (in this order) or what ?

    "[A-Z]{2}\\d{2}"

    Pattern (Java Platform SE 6)

  7. #7
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default

    Dear, the second one is not working :-(
    I dont know why but it does not taking any input................ no chars and no digits....
    Can i have to change the range value of that constructor too???

  8. #8
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    it is clear that this does not work!
    (it should work only if you copy and paste the whole correct string into the textfield!!!)

    You can customize the code or? You dont have to use String.matches(...) and a given Regex. You could iterate over the string, and check if the first character is an upper case, the second, and so on...

    or use two if statements.. like if((str.length() == 1 || str.length() == 2 ) && str.matches([A-Z+]) else if((str.length() == 3 || str.lenght() == 4 ) && str.matches(the pattern shown above)...

    or or or :) Be creative !

  9. #9
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default

    Hmm i will trying the same.....but not very familiar with java....
    By the way thanks alot...

  10. #10
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Try for example:
    Java Code:
        @Override
        public void replace(FilterBypass fb, int offs, int length, String str2, AttributeSet a)
                throws BadLocationException {
        	String str = fb.getDocument().getText(0, fb.getDocument().getLength()) + str2;
        	if((str.length() == 1 || str.length() == 2 ) && str.matches("[A-Z]+") || ((str.length() == 3 || str.length() == 4 ) && str.matches("[A-Z]{2}\\d{1,2}"))){
                super.replace(fb, offs, length, str2, a);
            } else {
               Toolkit.getDefaultToolkit().beep(); // :D
            }
        }
    :D

Similar Threads

  1. Reading Integers from a text file
    By tress in forum New To Java
    Replies: 6
    Last Post: 02-26-2011, 05:45 PM
  2. How to email formatted text from a JTextPane
    By akssingh400 in forum AWT / Swing
    Replies: 2
    Last Post: 02-23-2011, 07:59 PM
  3. Replies: 2
    Last Post: 08-21-2010, 10:53 AM
  4. file input: array of integers
    By hannes in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 03:44 PM
  5. Making arrays by reading user input
    By apfroggy0408 in forum New To Java
    Replies: 23
    Last Post: 04-30-2008, 01:23 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
  •