How to restrict inputs in jtextfield strictly to double values?
-
09-07-2021 - |
Frage
I am coding a gui and I wanted to use JFormattedTextField to verify my Input as double value, but the amountFormatter does not give me the right input I typed in back. So I tried to create an own Formatter with ("##########") which gives me a number with 10 digits, BUT i cannot verify double values(because they usually have a '.' in it)...
So my question is: How to simply verify double Values with Jformatted TextFields, or is there a much easier way with another Swing Component?
UPDATE Thx for your great answers!!! But is there possibly a solution with JFormatted TextField?
Lösung
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/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
More info:
http://puces-blog.blogspot.ch/2012/07/news-from-software-smithy-version-02.html
Homepage:
Download:
http://sourceforge.net/projects/softsmithy/files/softsmithy/
Maven:
<dependency>
<groupId>org.softsmithy.lib</groupId>
<artifactId>softsmithy-lib-core</artifactId>
<version>0.2</version>
</dependency>
Andere Tipps
this is basic reason for why JFormattedTextField is there,
set proper number formatter for
JFormattedTextField
, and /or withLocale
toopossible for JSpinner and JFormmatedTextField too
EDIT
you can start with
NumberFormat format = NumberFormat.getNumberInstance();
format.setGroupingUsed(false);
format.setGroupingUsed(true);// or add the group chars to the filter
format.setMaximumIntegerDigits(10);
format.setMaximumFractionDigits(2);
format.setMinimumFractionDigits(5);
format.setRoundingMode(RoundingMode.HALF_UP);