简体   繁体   中英

Swing: why does JFormattedTextField adds “,” on focus lost

the question is in the title.

I'm displaying an int number in my textfield but it keeps adding a "," when I exit the field... any ideas why?

for the code lovers:

the onfocuslost calls:

if(textStiffness != null){
            String s1 = textStiffness.getText();
            if(s1 != null){
                stiffness = Float.valueOf(s1.replaceAll(",", "")).intValue();
                stiffness = Math.max(0, stiffness);
            }
        }

then:

if(textStiffness != null){
            textStiffness.setText((""+(int)stiffness).replaceAll(",", ""));

        }

I checked the text set in the field and its correct 10000, but then it gets changed to 10,000 and I can't see why

You're still not showing us the NumberFormat that the JFormattedTextField is using, and this actually is the critical information necessary for solving your problem. I can only assume that you're using a NumberFormat.getNumberInstance() for the formatter, and if so, if you check the API for this class, you'll see that for this object, the groupingUsed property is set to true by default. You want to set it to false to get rid of your commas.

For eg here is my SSCCE that shows your problem and its solution:

import java.awt.BorderLayout;
import java.text.NumberFormat;

import javax.swing.*;

public class FormattedFieldFun {
   private static void createAndShowUI() {
      NumberFormat numberFormatGuFalse = NumberFormat.getNumberInstance();
      numberFormatGuFalse.setGroupingUsed(false);  // ***** HERE *****
      JFormattedTextField jftFieldGuFalse = 
          new JFormattedTextField(numberFormatGuFalse);

      NumberFormat numberFormatGuTrue = NumberFormat.getNumberInstance();
      // numberFormatGuFalse.setGroupingUsed(true); // not necessary as is default
      JFormattedTextField jftFieldGuTrue = 
          new JFormattedTextField(numberFormatGuTrue);

      JPanel panel = new JPanel(new BorderLayout());
      panel.add(jftFieldGuFalse, BorderLayout.NORTH);
      panel.add(jftFieldGuTrue, BorderLayout.SOUTH);

      JFrame frame = new JFrame("FormattedFieldFun");
      frame.getContentPane().add(panel);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

Reading the docs , I found some observations:

Note: Some formatters might update the value constantly, rendering the loss of focus meaningless, as the value is always the same as what the text specifies.

Note that although the JFormattedTextField class inherits the setText method from the JTextField class, you do not usually call the setText method on a formatted text field. If you do, the field's display changes accordingly but the value is not updated (unless the field's formatter updates it constantly).

and also the setFocusLostBehavior(int) :

Specifies the outcome of a field losing the focus. Possible values are defined in JFormattedTextField as COMMIT_OR_REVERT (the default), COMMIT (commit if valid, otherwise leave everything the same), PERSIST (do nothing), and REVERT (change the text to reflect the value).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM