Java : Does Locale affect the Formatter for JFormattedTextField

标签 java swing locale customization jformattedtextfield

我已经创建了 JFormattedTextField 的自定义组件 (NumberFormattedTextField)。这是我使用的格式化程序:

    public static InternationalFormatter getDecimalIFormatter92() {
    // For Buy & Sale Price
    DecimalFormat numberFormat = (DecimalFormat) DecimalFormat.getNumberInstance();
    numberFormat.setMaximumFractionDigits(2);
    numberFormat.setMinimumFractionDigits(2);
    numberFormat.setRoundingMode(RoundingMode.HALF_UP);
    numberFormat.setGroupingUsed(false);

    final InternationalFormatter formatter = new InternationalFormatter(numberFormat);
    formatter.setAllowsInvalid(false);
    formatter.setMinimum(0.00);
    formatter.setMaximum(999999999.99);

    return formatter;
}

当我创建 NumberFormattedTextField 实例时:

 public RelationsPanel(CashParent parent) {
    try {
    initComponents();   // startBalTxt = new NumberFormattedTextField();
    Utility.logInfo("initComponents OVER");
    myParent = parent;
    nameTxt.setSizeLimit(20);
    System.out.println("startBalTxt.setFormatter GOING FOR IT"); 
    this.startBalTxt.setFormatter(Utility.getDecimalIFormatter82());  // Must be throwing here
    System.out.println("startBalTxt.setFormatter DONE"); 
    this.currentBalTxt.setFormatter(Utility.getDecimalIFormatter82());
    } catch (Exception e) {
        Utility.logInfo("Failed to Initialize : " + e.getMessage());
        e.printStackTrace();
    }
}

NumberFormattedTextField 类代码:

public class NumberFormattedTextField extends JFormattedTextField implements java.io.Serializable, DocumentListener, FocusListener {
private DecimalFormat numberFormat;
private InternationalFormatter formatter;
private double MAX_VALUE = 999999.99;
private final Map attributes = (Utility.getTextFont()).getAttributes();

/**
 * Creates a NumberFormattedTextField with 6 Integer & 2 Fractional digits.
 * Minimum is set to 0.00 and Max to 999999.99 without any grouping used.
 */
public NumberFormattedTextField() {
    super();
    createFormatter();  // Creates a default formatter, used if formatter is not set
    this.setValue(0.00);
    init();
}


private void createFormatter() {
    // Create standard DecimalFormat
    numberFormat = (DecimalFormat) DecimalFormat.getNumberInstance();
    numberFormat.setMaximumFractionDigits(2);
    numberFormat.setMinimumFractionDigits(2);
    numberFormat.setRoundingMode(RoundingMode.HALF_UP);
    numberFormat.setGroupingUsed(false);

    formatter = new InternationalFormatter(numberFormat);
    formatter.setAllowsInvalid(false);
    formatter.setMinimum(0.00);
    formatter.setMaximum(999999.99);

    this.setFormatterFactory(new AbstractFormatterFactoryImpl());
}

private void init() {
    setFont(Utility.getTextFont());
    this.getDocument().addDocumentListener(this);
    this.addFocusListener(this);
    attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
    setFocusLostBehavior(PERSIST);
}

public void setFormatter(InternationalFormatter format) {
    super.setFormatter(format);
    System.out.println("Class of Format = " + format.getFormat().getClass());
    if (format.getFormat() instanceof java.text.DecimalFormat)
        numberFormat = (DecimalFormat)format.getFormat();
    else
        numberFormat = (DecimalFormat)(NumberFormat) format.getFormat();

    formatter = format;
    // AbstractFormatterFactoryImpl returns formatter straight away    
    this.setFormatterFactory(new AbstractFormatterFactoryImpl());
    calculateMaxValue();
}

private void calculateMaxValue() {
    try {
        if (formatter.getMaximum() != null) {
            //System.out.println(" MAX ALlowed = " + formatter.getMaximum());
            String no = formatter.valueToString(formatter.getMaximum());
            char seperator = java.text.DecimalFormatSymbols.getInstance().getGroupingSeparator();
            no = no.replace(String.valueOf(seperator), "");
            System.out.println("MAX Number to PArse = " + no);
            MAX_VALUE = Double.parseDouble(no);  // HERE ITS THROWING EXCEPTION
        }
    } catch (ParseException ex) {
        Logger.getLogger(NumberFormattedTextField.class.getName()).log(Level.SEVERE, null, ex);
    }
}

上述代码在我的电脑上运行良好。但是在客户端的 PC 上它给出了错误: 初始化失败:对于输入字符串:“99999999,99” 我得到的错误/日志:

      startBalTxt.setFormatter GOING FOR IT
  INFO: initComponents OVER
  Class of Format = class java.text.DecimalFormat
  MAX Number to PArse = 99999999,99
  INFO: Failed to Initialize : For input string: "99999999,99"
  java.lang.NumberFormatException: For input string: "99999999,99"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
    at java.lang.Double.parseDouble(Double.java:540)
    at cashaccountingapp.components.NumberFormattedTextField.calculateMaxValue(NumberFormattedTextField.java:184)
 Class of Format = class java.text.DecimalFormat
    at cashaccountingapp.components.NumberFormattedTextField.setFormatter(NumberFormattedTextField.java:173)
    at cashaccountingapp.data.RelationsPanel.<init>(RelationsPanel.java:35)

我们两个的语言环境不同。

这个错误的原因是什么?在我的 NumberFormattedTextField 类中需要注意的不同区域设置或其他内容。我希望应用程序适应系统设置并相应地显示。

如何解决问题??

最佳答案

最有可能(不能完全确定,因为您没有显示初始设置并且仅将错误削弱到其消息中;-)不同的区域设置是原因:一个将句点作为小数点,另一个将逗号。因此,如果您使用表示另一个中的数字的字符串初始化一个,它将发生冲突:逗号未被解释为小数点分隔符,因此最大值太大而无法满足限制

关于Java : Does Locale affect the Formatter for JFormattedTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8635480/

相关文章:

java - Swing GUI 和翻译器问题

java - 如何将 JButton 添加到 GridLayout?

java - 更改区域设置操作会导致 404

c++ - CUDA 内核 printf 的语言环境

java - 为不同的语言环境定制 java.text 格式化程序

java - Eclipse:为项目导出用户库/库的 Ant 脚本

java - Android 应用程序不会将数据存储在数据库中

java - 一个类的清理代码

java - 静态类变量存储在内存中的什么位置?

java - 为什么 Java GUI JFrame 在不同的操作系统上大小不同?