Total members 10261 | Gratitudes |It is currently Wed May 23, 2012 9:24 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 2 posts ] 
Author Question
 Question subject: Only numeric input in JTextField
PostPosted: Tue Oct 21, 2008 9:09 pm 
Offline
Proficient
User avatar

Joined: Sun Oct 19, 2008 3:47 pm
Posts: 281
Has thanked: 0 time
Have thanks: 1 time

Hi,

I'm trying to extend the JTextField class to a JNumField subclass in
which only numeric key-in would be accepted.
This is probably a very common problem, but I could not find a simple
solution to it yet.

I tried with a MaskFormatter, as explained here (see pjstroh's
message) but encountered problems when trying to compile the code.
http://forum.java.sun.com/thread.jspa?t ... ID=1876495

So, I tried somehting different by checking the keyTyped key event.
But this does not work. Does someone know why ?

Here's some code from my JNumField class:
Code:
// *** Initialisation ***
public void init() {
setBackground(Color.white);
addKeyListener(this);
hasDecimalPoint = false;
}

// *** Impl�mentation de l'interface KeyListener ***
public void keyTyped(KeyEvent ke) {
char c = ke.getKeyChar();
if (!Character.isDigit(c))
ke.consume(); // prevent event propagation
}

Thanks for any help.


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Only numeric input in JTextField
PostPosted: Tue Oct 21, 2008 9:18 pm 
Offline
Proficient
User avatar

Joined: Sun Oct 19, 2008 3:53 pm
Posts: 229
Has thanked: 0 time
Have thanks: 0 time
Solved. The problem was that I adding the key listener from within the
init() function which is -I assume- called only for applets.
As my class is not an applet, the init() function was not called.
I can now catch key events as I moved addKeyListener(this) in the
class constructor.


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 2 posts ] 
Quick reply


  

 Similar topics
 Google Chrome input file
 getting file name of html input file tag using jsp
 getting a full line input
 Get Form Numeric value
 Simple code for taking input from shell

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 4 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team