Anyone knows how to only allow characters in a JtextField not strings or digits? any example source code will be appreciated.

Recommended Answers

All 21 Replies

What does "only characters... not strings" mean?

but whatever, here's one way
http://www.devx.com/tips/Tip/14311

and the intro to the JavaDoc for JTextField shows another way - this one is an officially-approved approach

Anyone knows how to only allow characters in a JtextField not strings or digits? any example source code will be appreciated.

see this:http://www.daniweb.com/software-development/java/threads/73050 what you should get from this is there is only two ways, either wait until all text has been entered and then check to make sure that if follows your rules, or have a keybinder that will get each letter typed in the box as it gets typed and from their act appropriately. see keybindings to a textfield here:http://www.java2s.com/Code/Java/Swing-JFC/OverridingaFewDefaultTypedKeyBindingsinaJTextComponent.htm

"there is only 2 ways"...
there's always another way...

the one in JTextField's JavaDoc is neither of those (it extends the default model), but has the official stamp of approval.

oh it means that i should accept a b but not bd fg

"there is only 2 ways"...
there's always another way...

the one in JTextField's JavaDoc is neither of those (it extends the default model), but has the official stamp of approval.

lol yes yes sorry i wasnt thinking when i typed, but ofcourse there is more thanks for letting the OP know :)

oh it means that i should accept a b but not bd fg

why not have a simple check like so:

String text=jTextField1.getText();
if(text.length()>1) {
System.out.println("this is not allowed");
jTextField1.setText("");
} else {
....
}

oh it means that i should accept a b but not bd fg

Do you mean that the input can only be 1 character (or String of length 1), not any longer?

but the string could be anything not only ab it could be hello or world or anything.. there should be a more systematic way to accept only characters but not any string > length2 or numbers

yes thats right it could only be string length 1 but not more

yes thats right it could only be string length 1 but not more

check my edited post above.

OK, parallel post with your improved def.
Do you want to check that when the user types into the field? When the user pastes into the field? When Java code changes the field's text? When focus leaves the field? When the user presses OK?

ok what I mean is that lets say I have a textfield, which is used to create a table the text in the JTextField will be the name of the columns of the table.
E.g. if a enter a b the first column will be a the second one will be b. so column names can only be variables single letters such as s n h r....

if the user entered abc with no space or comma it should not accept it because abc cant be used as a column name..

hope this explains what im trying to do

ok what I mean is that lets say I have a textfield, which is used to create a table the text in the JTextField will be the name of the columns of the table.
E.g. if a enter a b the first column will be a the second one will be b. so column names can only be variables single letters such as s n h r....

if the user entered abc with no space or comma it should not accept it because abc cant be used as a column name..

hope this explains what im trying to do

String text=jTextField1.getText();
if(text.length()>1&&!text.contains(" ")&&!text.contains(",")) {//make sure that its length is not over 1, and that it has no spaces and no commas
System.out.println("this is not allowed");
jTextField1.setText("");
} else {//if a space or comma was found no matter how big the text it will execute the else
....
}

wont that work?

OK. In that case it's easiest just to validate the input when you need to use it. c0rthingy's edited version is a good place to start - test for the length being exactly 1, use charAt to get the one char from the String, then test that for being in the right range eg 'a' to 'z'

thanks for your help cOrRuPtG3n3t!x.. It seems to work I changed it slightly coz Im using patterns and regex to seperate the input

OK. In that case it's easiest just to validate the input when you need to use it. c0rthingy's edited version is a good place to start - test for the length being exactly 1, use charAt to get the one char from the String, then test that for being in the right range eg 'a' to 'z'

c0rthingy's? nice man, its not hard to scroll down and copy and paste a name hey

sorry for the wrong name:))

sorry for the wrong name:))

no not you nilay84 ,i was talking to JamesCherrill

oh ok than thanks for your help anyways..

c0rthingy's? nice man, its not hard to scroll down and copy and paste a name hey

Please, I intended no disrespect. I did try to copy it, but it's harder than you think - you can't double click to select the word because that links to your profile, and its near impossible to drag through it without selecting all kinds of adjacent stuff.
Anyway, I apologise if I caused any offense.
ps: I get the "corrupt", but what does the rest mean?

commented: not a prob man +6

Please, I intended no disrespect. I did try to copy it, but it's harder than you think - you can't double click to select the word because that links to your profile, and its near impossible to drag through it without selecting all kinds of adjacent stuff.
Anyway, I apologise if I caused any offense.
ps: I get the "corrupt", but what does the rest mean?

not a problem James :) its gentix (genetics) lol.no bad feelings and no disrespect taken

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.