1

latitude: 43,324523 longitude: 34,958140

I used IntegerField provided here IntegerField

I can parse the number to get the format what I like but I want it while in the frame screen. When user typing (without entering dot or comma)

But how I will be able to make it.

this is the frame I would like to edit

In this screen it should be changed

Like the second box, User won't enter the dots but this is not the format I like. When you read the code you will understand what I mean.

third screenshot was shot with this code

DecimalFormat format = new DecimalFormat("##,####");
format.setGroupingUsed(true);
NumberFormatter formatter = new NumberFormatter(format);
formatter.setValueClass(Integer.class);                  
formatter.setMaximum(Integer.MAX_VALUE);
formatter.setAllowsInvalid(false);

IntegerField minField = new IntegerField();
JFormattedTextField maxField = new JFormattedTextField(formatter);
Community
  • 1
  • 1
trd3v3lop
  • 323
  • 2
  • 15

4 Answers4

1

Ok I found solution. MaskFormatter deals with this problem very well. Still, any other ideas would be appreciated

MaskFormatter latitudeformat=new MaskFormatter("##.######");
MaskFormatter longitudeformat=new MaskFormatter("###.######");
latitudeformat.setPlaceholderCharacter('0');
latitudeformat.setOverwriteMode(true);
longitudeformat.setPlaceholderCharacter('0');
longitudeformat.setOverwriteMode(true);    

JFormattedTextField latitudeField = new JFormattedTextField(latitudeformat);
JFormattedTextField longituteField = new JFormattedTextField(longitudeformat);
trd3v3lop
  • 323
  • 2
  • 15
  • Longitude goes from -180 degrees to +180 degrees. You've only allotted 2 digits. – Gilbert Le Blanc Aug 09 '16 at 18:20
  • @GilbertLeBlanc yeah i know just copy pasted. but it is only the way workaround I found. also with this method I can't add write negative numbers.. Edited the answer. Btw sir do you know how to improve this answer to be really able to insert latitude and longitude in the extent of -90 to +90 and -180 to +180 – trd3v3lop Aug 09 '16 at 18:33
  • MaskFormatter is not designed for general double input. There's no mask character for + and - that I'm aware of. – Gilbert Le Blanc Aug 09 '16 at 18:38
0

latitudes and longitudes are not integer values i guess ;) use just two labled JTextFields one for longitudes and the other for latitudes

integers values are basically Strings of numbers from 0 to 9 and the minus to make floats you only need to accept the '.'

whyn0t
  • 301
  • 2
  • 14
  • but I want to make user could only enter number. so i used jformattedtextfield. – trd3v3lop Aug 09 '16 at 09:15
  • that's exactly what a text field is for =], a formatted text field is used when you have data that has a format like a date or time but to read a numeric value JTextField is more appropriate; all you have to do is this txtField1 = new JTextField(10 ); double value = Double.parseDouble(txtField1.getText()); – whyn0t Aug 09 '16 at 09:23
  • See [how to validate a jtextfield to accept only integer numbers](http://stackoverflow.com/questions/14319064/how-to-validate-a-jtextfield-to-accept-only-integer-numbers) – Nico T Aug 09 '16 at 09:32
  • No you got me wrong. In frame i want to permit the user to enter any string to the field. In your way user can enter a string. – trd3v3lop Aug 09 '16 at 09:33
  • @NicoT will try that but the question is formatting rather than accepting only integers :D – trd3v3lop Aug 09 '16 at 09:38
  • Edited the question now my question should be clear :) – trd3v3lop Aug 09 '16 at 09:54
0

The GUI would contain 2 JLabels and 2 JTextFields, arranged like your example.

Latitude: 43,324523    Longitude: 34,958140

You would test for doubles in the JTextFields with the following code:

public double convertTextToDouble(String text) {
    try {
        return Double.valueOf(text);
    } catch (NumberFormatException e) {
        return Double.MIN_VALUE;
    }
}

If the text is a valid double, you'll return the double value. If the text is not a valid double, you'll return the minimum double value.

Normally, you wouldn't use exceptions to test for invalid values. In this case, you should because the exception already exists.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • thanks for replying. but it is not what i want. I edit the post in order to be more clear. Sorry for the low skilled English. – trd3v3lop Aug 09 '16 at 12:36
0
MaskFormatter latitudeformat=new MaskFormatter("*##.######");
**latitudeformat.setValidCharacters("-+1234567890");**
MaskFormatter longitudeformat=new MaskFormatter("*###.######");
**longitudeformat.setValidCharacters("-+1234567890");**
latitudeformat.setPlaceholderCharacter('0');
latitudeformat.setOverwriteMode(true);
longitudeformat.setPlaceholderCharacter('0');
longitudeformat.setOverwriteMode(true);    
  • While this code may answer the question, providing information on how and why it solves the problem improves its long-term value. – L_J Jul 20 '18 at 11:06
  • The Mask passed to the constructor *##.###### expects the first character to be any character while the rest to be digits. More information on the this link https://docs.oracle.com/javase/7/docs/api/javax/swing/text/MaskFormatter.html. We further restrict the character to be inputted by setting further the valid characters. hence the first letter can only take a value of +- or any digit – Steve Nganga Jul 20 '18 at 12:56