473,408 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

JtextField - only numerical input

144 100+
hi,

i wonder a way to validate a JTextField to only receive numerical input. i've tried with the KeyListener and consumer() but it doesn't work. i've tried InputVerifier but it also doesn't work, maybe that because i don't really understand how inputVerifier works.

is there another way?
thx
Dec 6 '07 #1
4 3212
hi,

i wonder a way to validate a JTextField to only receive numerical input. i've tried with the KeyListener and consumer() but it doesn't work. i've tried InputVerifier but it also doesn't work, maybe that because i don't really understand how inputVerifier works.

is there another way?
thx
Are you using a rich application or a web application? Web applications can be handled via some javascript
Dec 6 '07 #2
heat84
118 100+
you can get the character and then check if its non-numeric , if not , you call consume(). e.g

char c=ke.getKeyChar();
if(!Character.isDigit(c)){
ke.consume(); // ignore the input
}
Dec 6 '07 #3
thesti
144 100+
hi,

no it's not a web app.
and yes i've tried consume(); but still the char appear in te textfield. here's the code of my action key listener

Expand|Select|Wrap|Line Numbers
  1. private class ValueKeyListener extends KeyAdapter{
  2.         public void keyPressed(KeyEvent e){
  3.  
  4.             if(e.getKeyCode() < KeyEvent.VK_1 && e.getKeyCode() > KeyEvent.VK_9){                
  5.                 e.consume();
  6.             }
  7.         }
  8.     }
  9.  
Dec 6 '07 #4
JosAH
11,448 Expert 8TB
Don't use low level KeyEvents for that, e.g. a paste operation (a ^V gesture) ruins
that entire little scenario. Use a JFormattedTextField instead (read the API
documentation).

kind regards,

Jos
Dec 6 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Alexander Mazo | last post by:
Hi! I'm new in JAVA. I'm wont change "Home" and "End", but nothing receive. public class RLTextField { public static String s = new String(); public static void main(String args) { JFrame...
31
by: JS | last post by:
We have the same floating point intensive C++ program that runs on Windows on Intel chip and on Sun Solaris on SPARC chips. The program reads the exactly the same input files on the two platforms....
2
by: Martin McCormick | last post by:
Most of the C programming I write manipulates strings so I haven't had much experience with numerical functions and passing variables in and out which brings me to a laughably simple problem that I...
0
JoeMac3313
by: JoeMac3313 | last post by:
The question is how can I update the JTextField in Class CreditDisplayField when I hit the button in class CreditButton? I wrote in a test printing to console to make sure the "logic.addCredits();"...
2
by: all eyes | last post by:
hi all, i am using here JTextField to get sum of all previously pressed numbers in one button called (+) and so on. there is only one button take care of addition and displaying the result in the...
1
by: ahmed222too | last post by:
the jtextfield donot accept Arabic language font when i run this code jtextfield.settext="ذهب الولد إلى المدرسة"; the arabic font change into another font in the jtextfield
4
by: ahmed222too | last post by:
when i read data from file by code into jtextfield the lines of the file apprear in Concatenation in the jtextfield (without Enters between lines) i want to transfere the content of the file to the...
3
by: Kid Programmer | last post by:
Hello guys. I was wondering if it is possible to make a JTextField read only. In a program I am writing I don't want the user to be able to edit the text in a JTextField.
1
by: dig909 | last post by:
I am trying to compare a number inserted into a jtextfield with a number generated with the program. Unfortunately the numbers never evaluate as equal. Here is an example of the code I'm working...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.