JTextField which only allows number input...

Hello,I'm not sure but is there any possibility to set a JTextField to only accept number input or is there any other component that only accepts numbers as input?Thanks in advance- Stephan
[225 byte] By [Jacklera] at [2007-11-27 9:37:30]
# 1

add key listener in textfield... use keyReleased and use the code below

char k_code = evt.getKeyChar();

if((Character.isDigit(k_code)) || (k_code==evt.VK_PERIOD))

text.setText(text.getText());

else

text.setText(text.getText().substring(0,text.getText().length()-1));

Yannixa at 2007-7-12 23:08:57 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hi,

I've implemented number fields based on JFormattedTextField.

They also support a min and a max value.

Maybe you find them useful (the library is open source):

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JRealNumberField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JDoubleField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JFloatField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedRealNumberField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedDoubleField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedFloatField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JWholeNumberField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JByteField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JIntegerField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLongField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JShortField.html

Tutorial:

http://softsmithy.sourceforge.net/lib/docs/tutorial/swing/number/index.html

Homepage:

http://www.softsmithy.org

Download:

http://sourceforge.net/project/showfiles.php?group_id=64833

Source:

http://sourceforge.net/svn/?group_id=64833

http://softsmithy.svn.sourceforge.net/viewvc/softsmithy/trunk/lib/src/org/softsmithy/lib/

-Puce

Pucea at 2007-7-12 23:08:57 > top of Java-index,Desktop,Core GUI APIs...
# 3
Puce is also right... I think you should use JFormattedTextFieldI always use JFormattedTextField when it comes to formatting inputs..
Yannixa at 2007-7-12 23:08:57 > top of Java-index,Desktop,Core GUI APIs...
# 4
Thanks guys for your answers - I'll take a look at it...- Stephan
Jacklera at 2007-7-12 23:08:57 > top of Java-index,Desktop,Core GUI APIs...