JTextField accept only numbers and one dot
NickName:Dani Ask DateTime:2017-04-18T21:07:42

JTextField accept only numbers and one dot

I want that my text field accept only numbers (digit) and one dot, because it is a field in which the user can write the price of products. I have this code but it doesn't work well, it only accept numbers and delete.

char c=evt.getKeyChar();
if(!(Character.isDigit(c))||(c==KeyEvent.VK_BACK_SPACE)||(c==KeyEvent.VK_DELETE)){
    getToolkit().beep();
    evt.consume();
}

Can someone help me to fix it?

Copyright Notice:Content Author:「Dani」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43473110/jtextfield-accept-only-numbers-and-one-dot

Answers
Dani 2017-04-20T12:16:42

I found a solution for this problem:\nthis is the code i wrote\n\n\r\n\r\nchar c=evt.getKeyChar();\r\n if((Character.isDigit(c))||(c==KeyEvent.VK_PERIOD)||(c==KeyEvent.VK_BACK_SPACE)){\r\n int punto=0;\r\n if(c==KeyEvent.VK_PERIOD){ \r\n String s=pricefield.getText();\r\n int dot=s.indexOf('.');\r\n punto=dot;\r\n if(dot!=-1){\r\n getToolkit().beep();\r\n evt.consume();\r\n }\r\n }\r\n }\r\n else{ \r\n getToolkit().beep();\r\n evt.consume();\r\n }",


camickr 2017-04-18T14:05:29

Don't use a KeyListener. That is old code when using AWT.\n\nSwing has newer and better API's.\n\nThe easiest way is to use a JFormattedTextField. Read the section from the Swing tutorial on How to Use Formatted Text Fields for more information and working examples.\n\nThe other option is so use a DocumentFilter. Read the section from the Swing tutorial on Implementing a DocumentFilter.",


helloWorld 2020-08-17T13:33:13

private void textFieldKeyTyped(java.awt.event.KeyEvent evt) {\n\nGet the keyTyped and pass it to the method validate.\n\n if(!validate(evt.getKeyChar())){\n\n\nChar or keytyped is not a valid input, so let it consumed.\n\n evt.consume();\n }\n\n\nthis limit the input of Decimal, only ONE decimal point can be entered;\n\n if(evt.getKeyChar()==KeyEvent.VK_DECIMAL || evt.getKeyChar()==KeyEvent.VK_PERIOD){\n \n\n\nget the whole string entered in the textField;\n\n String field = textField.getText();\n\n\nget index of dot(. or decimal/period).\nindexOf() method returns -1 if String does not have any dot or decimal poits.\n\n int index = field.indexOf(".");\n\n\n if(!(index==-1)){ //index is not equal to -1\n evt.consume(); //consume\n }\n \n }\n } \n\n\nEvery keypress, this method gets called.\n\nprivate boolean validate(char ch){\n\n\nthis determine if the character has matching value to Integer, Decimal point, backspace or delete. It returns true if the char is a integer, decimal, delete or backspace, otherwise false.\nHowever it does not limit how many decimal points can be entered.\n\n if(!(Character.isDigit(ch)\n || ch==KeyEvent.VK_BACKSPACE \n || ch==KeyEvent.VK_DELETE \n || ch==KeyEvent.VK_DECIMAL\n || ch==KeyEvent.VK_PERIOD)){\n return false;\n }\n \n return true;\n } \n\n\nhere is the whole code, I have provided some comments, hope these will help.\n\nprivate void textFieldKeyTyped(java.awt.event.KeyEvent evt) { \n\n if(!validate(evt.getKeyChar())){ //get char or keytyped\n evt.consume();\n }\n //limit one dot or decimal point can be entered\n if(evt.getKeyChar()==KeyEvent.VK_DECIMAL || evt.getKeyChar()==KeyEvent.VK_PERIOD){\n \n String field = textField.getText(); //get the string in textField\n int index = field.indexOf("."); //find the index of dot(.) or decimal point\n if(!(index==-1)){ //if there is any\n evt.consume(); //consume the keytyped. this prevents the keytyped from appearing on the textfield;\n }\n \n }\n } \n //determine if keytyped is a valid input\n private boolean validate(char ch){\n \n if(!(Character.isDigit(ch)\n || ch==KeyEvent.VK_BACKSPACE \n || ch==KeyEvent.VK_DELETE \n || ch==KeyEvent.VK_DECIMAL \n || ch==KeyEvent.VK_PERIOD \n )){\n\n return false; //return false, because char is invalid \n }\n \n return true; // return true, when the if statement above does not meet its conditions \n } \n",


More about “JTextField accept only numbers and one dot” related questions

JTextField accept only numbers and one dot

I want that my text field accept only numbers (digit) and one dot, because it is a field in which the user can write the price of products. I have this code but it doesn't work well, it only accept

Show Detail

jTextField accept only one dot in java calculator?

I have created a simple calculator in java using net beans. In this calculator when user accidentally press dot button(jButton) multiple time textfield shows many dots as user pressed. for example if

Show Detail

Accept only numbers and a dot in Java TextField

I've got one textField where I only accept numbers from the keyboard, but now I have to change it as it's a "price textField" and I would also need to accept a dot "." for any kind of prices. How ...

Show Detail

Accept only numbers and a dot in Java TextField

I've got one textField where I only accept numbers from the keyboard, but now I have to change it as it's a "price textField" and I would also need to accept a dot "." for any kind of prices. How ...

Show Detail

UWP Textbox should accept only one dot

The textbox should only accept numbers and one . value. However, with my code, the input of numbers is also rejected as soon as I put a dot. I don't understand why, because in the if loop I do a ch...

Show Detail

PHP regex accepts only letters and numbers and only one dot

I don't know much about regex and I'm trying to create a username string accepts only English letters and numbers and only one dot. I have this regex code: if(preg_match('/[^A-Za-z0-9]/', $userna...

Show Detail

jTextField validation for numbers and one decimal point?

I need to set a jTextField to accept only numbers and one decimal point.Nothing else. Decimal point can't be typed more than once and no other characters are allowed. How can I do this? I typed this

Show Detail

Common method for the JTextFields to accept only numbers

I limit my JTextFields to accept only numbers. I was using following code for this. // my textboxes t1=new JTextField(10); t2=new JTextField(10); t3=new JTextField(10); // for the first...

Show Detail

Valide comma or dot Jtextfield

as allowing the dot or comma, I use validation numbers, but I need to allow comma and period (dot). public static void lNum(JTextField txt){ txt.addKeyListener(new KeyAdapter(){ @Overr...

Show Detail

Make JTextField accept only one digit

I'm working on a Sudoku applet, I want to make the cells (extends JTextField) of it so it would accept only integers between 0-9 and length of 1. Later I'll limit it even more (so it would fit the ...

Show Detail