Results 1 to 10 of 10
- 05-11-2011, 04:20 PM #1
Member
- Join Date
- May 2011
- Posts
- 56
- Rep Power
- 0
- 05-11-2011, 04:35 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Use a DocumentFilter!
Example to use: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 } } }
Java Code:...... JTextField textfield = new JTextField(4); ((AbstractDocument) textfield.getDocument()).setDocumentFilter(new DocumentSizeFilter(4, "\\d+"));
- 05-11-2011, 04:50 PM #3
Member
- Join Date
- May 2011
- Posts
- 56
- Rep Power
- 0
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???
- 05-11-2011, 05:04 PM #4
Member
- Join Date
- May 2011
- Posts
- 56
- Rep Power
- 0
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
- 05-11-2011, 05:19 PM #5
Member
- Join Date
- May 2011
- Posts
- 56
- Rep Power
- 0
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
- 05-11-2011, 05:24 PM #6
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
only two upper case letters and two digits (in this order) or what ?
"[A-Z]{2}\\d{2}"
Pattern (Java Platform SE 6)
- 05-11-2011, 05:35 PM #7
Member
- Join Date
- May 2011
- Posts
- 56
- Rep Power
- 0
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???
- 05-11-2011, 05:47 PM #8
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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 !
- 05-11-2011, 05:59 PM #9
Member
- Join Date
- May 2011
- Posts
- 56
- Rep Power
- 0
Hmm i will trying the same.....but not very familiar with java....
By the way thanks alot...
- 05-11-2011, 06:12 PM #10
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Try for example:
:DJava 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 } }
Similar Threads
-
Reading Integers from a text file
By tress in forum New To JavaReplies: 6Last Post: 02-26-2011, 05:45 PM -
How to email formatted text from a JTextPane
By akssingh400 in forum AWT / SwingReplies: 2Last Post: 02-23-2011, 07:59 PM -
How to create UPPERCASE formatted text field with mixed character input
By r00tb33r in forum AWT / SwingReplies: 2Last Post: 08-21-2010, 10:53 AM -
file input: array of integers
By hannes in forum New To JavaReplies: 8Last Post: 01-27-2010, 03:44 PM -
Making arrays by reading user input
By apfroggy0408 in forum New To JavaReplies: 23Last Post: 04-30-2008, 01:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks