Skip to main content

Need help with an input validation in a swing (JtextField)

No replies
bravo_dms
Offline
Joined: 2013-05-20
Points: 0

I have made a java program in which I want the program to take input
from a user in a GUI frame (swing) and store in a .dat file in row and
columns. The problem is with this particular code as it is not giving
the desired results. the user should be able to enter only numbers
between 20 to 30 using a keylistener. the input should not accept any
number that is out of this range, i.e, 19 or 31. Can some help be provided. In eclipse it is showing 'invalid character constant'. I don't want anything other than the keylistener element as this seems to be simple yet effective.

// input validation that restricts user to enter specific number range
between 20 and 30

txDeposit.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
char c = ke.getKeyChar();
if(!(ke.getKeyChar() >= '20' && ke.getKeyChar() <= '30')) {
getToolkit().beep();
ke.consume();
}
}
});