0

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

3
Contributors
21
Replies
23
Views
5 Years
Discussion Span
Last Post by DavidKroukamp
Featured Replies
  • [QUOTE=cOrRuPtG3n3t!x;1744220]c0rthingy's? nice man, its not hard to scroll down and copy and paste a name hey[/QUOTE] 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 … Read More

0

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

Edited by DavidKroukamp: n/a

0

"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.

0

"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 :)

0

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 {
....
}

Edited by DavidKroukamp: n/a

0

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?

0

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

0

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

0

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

check my edited post above.

Edited by DavidKroukamp: n/a

0

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?

Edited by JamesCherrill: n/a

0

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

0

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?

Edited by DavidKroukamp: n/a

0

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'

0

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

0

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

Edited by DavidKroukamp: n/a

1

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?

Comments
not a prob man
1

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.