Swing: keyListener codes are "unknown" - java

I put a KeyListener on a TextField in my swing application to try some functionalities. The goal is to react on every key typed in that TextField. The user should only type in numbers, but how it is, it is possible to enter alphabetical chars too. So additionally I have to check every time after a key is typed, if the whole thing is a number, if so, make something with that number, if not, tell the user there is an error without exit the program. So I want to do something like this:
String enteredNumPlayers = "";
JTextField textfieldNumPlayers = new JTextField();
textfieldNumPlayers.setBounds(/*some values*/);
textfieldNumPlayers.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.DARK_GRAY));
textfieldNumPlayers.setHorizontalAlignment(JTextField.CENTER);
textfieldNumPlayers.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
if(e.getKeyChar()!=/*Code of the back key*/){
enteredNumPlayers += e.getKeyChar();
System.out.println(e);
}else{
enteredNumPlayers = enteredNumPlayers.substring(0, s1.length()-1);
}
try{
Integer.parseInt(enteredNumPlayers);
// do something with that number
}catch (NumberFormatException err){
new ErrorDialog("Not a number"); // my own method to allude user
}
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
});
Now I wanted to look whats the specific Code for the back key by simply System.out.println(e) in the keyTyped(...) method, but following get printed:
java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unbekannt keyCode: 0x0,keyChar=Rücktaste,keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0,extendedKeyCode=0x0] on (...)
why is every key value or code = 0 or unknown? Shouldn't it be the ascii value? Using "Rücktaste" would also be ugly, since on a english working computer this value would be different, isn't it? So how can I cleanly check if the key typed is the back key?
The same thing happens with other characters, except that their keyChar is the right one.

What you are looking for is a DocumentFilter, DocumentListener or a JFormattedTextField. All three of them are a better solution then using a key listener as it also covers drag-and-drop, copy-paste or any other mechanism you can think of to put text into a textfield.

I think that having an error message to show each time the user presses anything but an integer is a bit annoying. Check this sample code that uses a regular expression to match the input and has the advantage of accepting only digits, anything else is just not inserted to the textfield (managed by overriding the insertString method of PlainDocument).

Related

Having trouble clearing a text field when an improper character is entered

I am trying to retrieve the values from a JTextField on a keypress to do something if the values are integers and to clear the field if the values are not integers. Every time I try to retrieve the value, I am getting the value entered before that(if I enter 12 I get 1 back then if I enter 123 I get 12 back) and when I try to clear the field on an invalid character everything but the invalid character gets cleared?
public void setUpListeners()
{
JTextField jT [] = myV.getTextFields();
jT[0].addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e){
int id = e.getID();
if (id == KeyEvent.KEY_TYPED)
{
char c = e.getKeyChar();
try
{
//check if chars entered are numbers
int temp = Integer.parseInt(String.valueOf(c));
String tempS = jT[0].getText();
System.out.println(tempS);
}
catch(Exception ex)
{
jT[0].setText("");
System.out.println("Not an integer");
}
}
}
});
}
You could do the following:
public static boolean validateNumber(char num){
return (num >= '0' && num <='9');
}
And then use parameter KeyEvent "e":
if(!validateNumber(e.getKeyChar()))
e.consume();
And instead of using
public void keyTyped(KeyEvent e){
use
public void keyReleased(KeyEvent e){
I think you will get what you need, I hope I have helped you ;)
Swing components use Model-View-Controller (MVC) [software] design pattern / architecture. The model holds the data. For a JTextField the data is the text it displays. The default model for JTextField is PlainDocument, however JTextField code actually refers to the Document interface which PlainDocument implements.
Look at the code for method getText() in class JTextComponent (which is the superclass for JTextField) and you will see that it retrieves the text from the Document.
When you type a key in JTextField, the character gets passed to the Document. It would appear that your keyTyped() method is invoked before the character you typed reaches the Document, hence when you call getText() in your keyTyped() method, you're getting all the text apart from the last character you typed.
That's one of the reasons not to use a KeyListener in order to validate the text entered into a JTextField. The correct way is detailed in the question that tenorsax provided a link to in his comment to your question.
Apart from DocumentFilter or JFormattedTextField (or even InputVerifier), you can add an ActionListener to JTextField which will execute when you hit Enter in the JTextField. You will find many on-line examples of how to implement each one of these options, including the link provided in tenorsax comment.

JTextField's value disorientation on clearing

I am trying to create a Java windows application using the WindowBuilder plug-in in Eclipse. I have a JTextField that works fine, on entry of the required values. But, when I clear all the contents on the field and try to re-enter other values, the values are disoriented i.e., the values don't appear at all, or seem to appear in the next line.
I'm using a DocumentListener, so yes, this might result in an empty string exception. But, I'm not sure why the contents of the JTextField are inappropriate on re-entry.
PFB my piece-of-code:
void computeTotal(final JTextField jtf){
jtf.getDocument().addDocumentListener(new DocumentListener(){
public void changedUpdate(DocumentEvent e) {
//warn();
getTotal();
}
public void removeUpdate(DocumentEvent e) {
//warn();
getTotal();
}
public void insertUpdate(DocumentEvent e) {
//warn();
getTotal();
}
public void getTotal(){
bt = Float.parseFloat(billTotal.getText());
bd = Float.parseFloat(billDeduction.getText()) ;
ta = Float.parseFloat(taxAmt.getText());
tt = bt - bd+ ta;
totalTotal.setText(tt+"");
}
});
}
What is the problem here?
What is the problem here?
use JFormattedTextField formatted by Number Formatter, then is DocumentListener correct listener
no parsing needed, no issue with nice output, thoushand separator, input of non number chars arenot allowed
use DocumentFilter with Patern, there you can remove non number chars and then you can parse with quite save contens contains numbers, decimal separator and negative sing

Generate AWT keyevent for special characters

I want to generate key events for Special characters like £, €, µ, ½, Ö, Ä etc. I am able to generate keyevents for key which are on my keyboard like 'A,B,c, %, *, ^' etc with following code:
public static void generateKeyEvent(final int c) {
new Thread() {
public void run() {
try {
Robot robot = new Robot();
robot.keyPress(c);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
robot.keyRelease(c);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
In case of normal characters, it is working fine but in case of characters which i mentioned above the code is throwing following exception:
java.lang.IllegalArgumentException: Invalid key code
at sun.awt.windows.WRobotPeer.keyPress(Native Method)
at java.awt.Robot.keyPress(Unknown Source)
at com.companyname.utils.Abc$1.run(Abc.java:286)
One thing which i noticed during my search for the solution to this problem, as these special characters are not mapped on my keyboard that is why it is throwing this exception.
any idea, how can i do this?
I got answer to this problem.. basically if you want to print symbol like those then you need to "alt" key for typing that.
for example: if you need to type 'é' in notepad you have to type alt+130.
So i did the same, i generated the key event for alt then for numpad 1 then numpad3 and finally numpad0.
How are you passing the keys?
Note that Robot.keyPress expects key code, not character. Take a look at the KeyEvent constants. There is a VK_EURO_SIGN, not sure about the others.
You should be able to get an arbitrary key code by implementing a KeyListener and checking KeyEvent.getKeyCode() when the particular key (combination of keys) is pressed.

How to limit user to input only numbers?

I am currently creating this java GUI that will ask the user to input 10 entries, then use the values to execte the next action.
I want only numbers or decimal point to be inputted inside such that it can only be a float value.
If it is not number or decimal point, it should prompt the user to input that specific entry again before the next action is executed.
How should I do it?
Wong,
not sure whether you are using Swing or not...
Ages ago I had the same problem and I solved it with creating a class RestrictedTextField extending JTextField. In the constructor I added a key listener (addKeyListener(new RestrictedKeyAdapter());)
private class RestrictedKeyAdapter extends KeyAdapter {
#Override
public void keyReleased(KeyEvent e) {
if (getText().equals("")) {
oldString = "";
return;
} else {
// if you cannot parse the string as an int, or float,
// then change the text to the text before (means: ignore
// the user input)
try {
if (type.equals("int")) {
int i = Integer.parseInt(getText());
oldString = getText();
} else if (type.equals("float")) {
float f = Float.parseFloat(getText());
oldString = getText();
} else {
// do nothing
}
} catch (NumberFormatException el) {
setText(oldString);
}
// if the text is identical to the initial text of this
// textfield paint it yellow. If the text was changed
// paint it red.
if (initialString.equals(getText())) {
setForeground(Color.YELLOW);
} else {
setForeground(Color.RED);
}
}
}
}
The idea is, that every time the user presses a key in the textfield (and releases it then), the text in the textfield is parsed. If the component should accept only floats for example then the component tries to parse it as an float (Float.parseFloat(..)). If this parsing is successful everything is fine. If the parsing fails (an NumberFormatException is thrown) then the old text is written back into the textfield (literally ignoring the user input).
I think you can add the KeyAdapter directly to the JTextField without creating a dedicated class for that, but with this solution you can remember the initial string and the old string.
you can play around with the code.. you can change the colour of the textfield if the input is valid or not (or like in my code snippet if the text is identical to the initial string).
one additional comment: I set the 'type' of the textfield in a variable with the name 'type', which is simply a String with the values "int", "float", etc.... a better solution would be here for example an enum of course...
I hope this is helpful...
timo
There are various options for what you would like to do. You can check here for one example of doing so. Another example could be to use Formatted TextFields, as shown here.
On the other hand, upon submission, you can try to parse the value to a float or double. If you get any exceptions, then, the value is not a number.
Lastly, you can use Regular Expressions. An expression such as ^\\d+(\\.\\d+)?$ should match any integer or floating point number.

How to validate if Text entered is a numeric number?

I have a calculation application which I need to validate the fields to check if the values entered are numeric numbers and not alphanumeric. I have some ideas about the codes.
Please guide me if I have done anything wrong or seem noob as this is my first time trying out Swing.
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
String text1 = jTextField1.getText(); // TODO add your handling code here:
}
private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
String text2 = jTextField2.getText(); // TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (text1 and text2 != <numeric number>){
JOptionPane.showConfirmDialog(null, "Please enter numbers only", "naughty", JOptionPane.CANCEL_OPTION);
}
// First we define float variables.
float num1, num2, result;
// We have to parse the text to a type float.
num1 = Float.parseFloat(jTextField1.getText());
num2 = Float.parseFloat(jTextField2.getText());
// Now we can perform the addition.
result = num1+num2;
// We will now pass the value of result to jTextField3.
// At the same time, we are going to
// change the value of result from a float to a string.
jTextField3.setText(String.valueOf(result));
// TODO add your handling code here:
}
Please do help. By the way why does my NetBeans keep informing me that it does not recognize the "JOptionPane" Command?
Float.parseFloat() will throw a NumberFormatException if the String isn't numeric and cannot be parsed into a Float. You can add a try-catch block to check for this condition:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
float num1, num2, result;
try {
num1 = Float.parseFloat(jTextField1.getText());
num2 = Float.parseFloat(jTextField2.getText());
result = num1+num2;
jTextField3.setText(String.valueOf(result));
} catch (NumberFormatException e) {
JOptionPane.showConfirmDialog(null, "Please enter numbers only", "naughty", JOptionPane.CANCEL_OPTION);
}
}
If alphanumeric input is not valid for the Swing component in the first place, then instead of validating this post-entry, you should restrict the component to accept only certain format in the first place.
Using the formatters that Swing provides, you can set up formatted text fields to type dates and numbers in localized formats. Another kind of formatter enables you to use a character mask to specify the set of characters that can be typed at each position in the field. For example, you can specify a mask for typing phone numbers in a particular format, such as (XX) X-XX-XX-XX-XX.
That said, you can, among other things, use Integer.parseInt(String s) to see if an arbitrary string can be parsed into an int; the method throws NumberFormatException if it can't. There are also Double.parseDouble, etc.
See also
Java Tutorials/Swing/How to use Formatted Text Field
How to use the Focus Subsystem/Input Validation
Java Tutorials/Internationalization/Formatting - Numbers and Currencies
Related questions
A simple way to create a text field (or such) that only allows the user to enter ints/doubles in Java?
A textbox class only accept integers in Java
Validating an integer or String without try-catch - java.util.Scanner option
try {
Integer.parseInt(foo);
} catch (NumberFormatException e) {
// Naughty
}
Try this:
String temp = txtField.getText();
try
{
int val = Integer.parseInt(temp);
}
catch(NumberFormatException e) {
System.out.println("Invalid");
}
To make it more enjoyable, use JOptionPane (makes it more more interactive)
textFieldCrDays = new JTextField();
textFieldCrDays.addKeyListener(new KeyAdapter() {
//// validate onlu numeric value
public void keyTyped(KeyEvent e) {
if (textFieldCrDays.getText().length() < 3 && e.getKeyChar() >='0' && e.getKeyChar() <= '9')
{
// Optional
super.keyTyped(e);
}
else
{
// Discard the event
e.consume();
}
}
});
A relatively old question, but I figured I would take a shot at it, to maybe help out the random Google Searches.
Another approach someone could take to minimise code and reduce the number of additional classes is to add a KeyListener for the keyType event and check for the Char value. This isn't very portable (you can't use region specific formatting such as numerical punctuation), but this could be quite helpful for straight integers.
You could also do a relative length here as well:
textField.addKeyListener(new KeyAdapter()
{
#Override
public void keyTyped(KeyEvent keyEvent)
{
if (textField.getText().length() < 3 && keyEvent.getKeyChar() >= '0' && keyEvent.getKeyChar() <= '9')
{
// Optional
super.keyTyped(keyEvent);
}
else
{
// Discard the event
keyEvent.consume();
}
}
});
You can also add another event listener to validate the entire integer for further processing (the entire number must be > 800 and < 5220 for example).
A good place for this would be on the focusLost event(?).
If you are doing these features frequently, it would be best to subclass the JTextField class to provide this functionality.
EDIT: Using Character.isLetter(keyEvent.getKeyChar()) is even more clear.

Categories

Resources