How to prevent input of letters and use of a space in Java?
Closed due to the fact that the essence of the question is unclear by the participants of pavel , Nofate ♦ 1 Sep '16 at 17:04 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
- 2Where exactly? Which UI? - Chubatiy
|
1 answer
You can use the ban on letters and space, the user can enter only numbers:
JTextField textField = new JTextField(10); textField.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); if ( ((c < '0') || (c > '9'))) { e.consume(); // игнорим введенные буквы и пробел } } });
If you need any characters, you can find their code on the Oracle page and add it to the if
:
if ( ((c < '0') || (c > '9')) && (c != KeyEvent.VK_PLUS)) { e.consume(); // разрешаем цифры и знак "+" }
- oneAnd why did you decide that the author is not a regular console application? - Alexey Shimansky
|