0

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?

E-Riz
  • 31,431
  • 9
  • 97
  • 134
maximus
  • 11,264
  • 30
  • 93
  • 124
  • 3
    Have you considered a `JSpinner` with appropriate model? Also note it is called Swing, not SWING. No need to SHOUT about it. – Andrew Thompson Oct 19 '12 at 15:56
  • 1
    Another possible solution is to use a `DocumentFilter` for this. There are plenty of examples of this on this site, including some by me. For example: [here](http://stackoverflow.com/a/11093360/522444) – Hovercraft Full Of Eels Oct 19 '12 at 15:58
  • Is there possibly a solution with JFormatted TextField? btw. sorry for shouting out Swing, just edited it;) – maximus Oct 19 '12 at 15:58

2 Answers2

2

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);
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

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

More info:

http://puces-blog.blogspot.ch/2012/07/news-from-software-smithy-version-02.html

Homepage:

http://www.softsmithy.org

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>   
Puce
  • 37,247
  • 13
  • 80
  • 152
  • 2
    Why would you need extensions of `JFormattedTextField` for each number types when you can achieve the same by providing the correct `Format` – Robin Oct 19 '12 at 16:21
  • @Robin in most cases doesn't works, JSpinner and JFormmatedTextField are evil – mKorbel Oct 19 '12 at 16:27
  • @Robin In my experience it's not so easy. E.g. the fields have a set/getXyzValue depending on the types. Also min and max values are depending on the data types. You can find the formatters and the factories here: http://softsmithy.sourceforge.net/lib/current/docs/api/softsmithy-lib-core/org/softsmithy/lib/swing/text/package-frame.html – Puce Oct 19 '12 at 22:46
  • Then we have different experiences. I used one extension of `JFormattedTextField` (to solve some usability issues) for longs, doubles, integers, strings, scales (e.g. 1 : 100000 ), ... . All by simply adjusting the format – Robin Oct 20 '12 at 08:59
  • Well, I just had a quick look at the source code again (I wrote those components years ago). They are quite thin. They just make sure the correct min, max and formatter is used and provide convenience methods. E.g. JByteField will work on primitve byte data types: http://softsmithy.hg.sourceforge.net/hgweb/softsmithy/lib/main-golden/file/ae786193023d/softsmithy-lib-core/src/main/java/org/softsmithy/lib/swing/JByteField.java http://softsmithy.hg.sourceforge.net/hgweb/softsmithy/lib/main-golden/file/ae786193023d/softsmithy-lib-core/src/main/java/org/softsmithy/lib/swing/text/ByteFormatter.java – Puce Oct 20 '12 at 10:22