Java prog#140. How to make textfield that only accepts numbers

ProgrammingKnowledge · 134K views · 2013 · 9:26

Open on YouTube

Software practice, open source & careersPython, Ruby & scripting languagestutorial

Why Stack Overflow recommends it

Stack Overflow users link it when asked how to restrict a Java text field to numbers, as a worked example of filtering key events.

Recommended by 0 people across 0 different months, 2014 to 2018.

What it is

A short Java Swing tutorial from ProgrammingKnowledge showing how to build a text field that only accepts numeric input. It uses a KeyTyped event handler that checks each character and discards non-digits.

What people said

Cites Brownman Revival · 2014-11 · Stack Overflow · on “input in text field only number in java application”

What i did is in the design mode i right click the text field > Events > Key > KeyTyped then in the code i hade something like this private void DishFormPanelistIdTxtKeyTyped(java.awt.event.KeyEvent evt) { // TODO add your handling code here: char enter = evt.getKeyChar(); if(!(Character.isDigit(enter))){ evt.consume(); } } i didnt get the solution here in stack but i will link it this is the link [this video]

Cites Oldmax · 2016-01 · Stack Overflow · on “netbeans - Textfield accepting only doubles”

Thank you NonameSL, Yea i am new to java. I am Learning with your comments. Yes your code is working. But the problem is it allowing Strings. I do not need to put Letters in to my textfield. Your code allowing like this inputs: 2.aaa, b.90 etc... I only need like this: 50.25, 10.75, 1.50 Exactly like: Rs. 1.50, 1.50$ etc... Hope you got it. I used this textfiled for store some price on database. My interface allowed double values. That is i want. Please look this: [this video] it is about it. Thank you for your time. Hope you will fix it.

Cites Simple · 2018-04 · Stack Overflow · on “How to make TextField which only accepts numbers in Codename One?”

I already took a look at those examples, but this is not what I'm looking for, because those are constraints. Constraints don't limit input type, it just hints on the virtual keyboard type. I'm looking for something like this: [this video]