[英]JFormattedTextField currency format for specific locale
i have used the information in the below link to display the currency format for specific locale : 我已使用以下链接中的信息显示特定语言环境的货币格式:
How to change the format of a JFormattedTextField at runtime? 如何在运行时更改JFormattedTextField的格式?
But the format is not changing, when changing the locale. 但是,更改语言环境时,格式不会更改。
here is the code. 这是代码。
package testapp;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;
/**
*
* @author Sanjeev
*/
public class JFormattedTextFieldAndCurrency extends javax.swing.JFrame {
private class LocaleInfo implements Comparable<LocaleInfo> {
private Locale locale;
public Locale getLocale() {
return locale;
}
public void setLocale(Locale locale) {
this.locale = locale;
}
@Override
public int compareTo(LocaleInfo localeInfo) {
return this.locale.getDisplayLanguage().compareTo(localeInfo.locale.getDisplayLanguage());
}
@Override
public String toString() {
return locale.getDisplayLanguage() + " " + locale.getDisplayCountry();
}
}
public JFormattedTextFieldAndCurrency() {
initComponents();
loadLocales();
localeCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateField();
}
});
this.setLocationRelativeTo(null);
this.setTitle("JFormatted Text Field & Currency");
updateField();
}
private void loadLocales() {
DefaultComboBoxModel<LocaleInfo> localeModel = new DefaultComboBoxModel<>();
localeCombo.setModel(localeModel);
ArrayList<LocaleInfo> localeInfos = new ArrayList<>();
for (Locale locale : Locale.getAvailableLocales()) {
LocaleInfo localeIno = new LocaleInfo();
localeIno.setLocale(locale);
localeInfos.add(localeIno);
}
Collections.sort(localeInfos);
for (LocaleInfo localeInfo : localeInfos) {
localeModel.addElement(localeInfo);
}
infoLabel.setText(localeModel.getSize() + " Locales available.");
}
private void updateField() {
LocaleInfo localeInfo = (LocaleInfo) localeCombo.getSelectedItem();
NumberFormatter currencyFormatter = new NumberFormatter(NumberFormat.getCurrencyInstance(localeInfo.getLocale()));
DefaultFormatterFactory currencyFormatterFactory = new DefaultFormatterFactory(currencyFormatter, currencyFormatter, currencyFormatter);
currencyField.setFormatterFactory(currencyFormatterFactory);
currencyField.setText("1234567.89");
System.out.println("currency format set to : "+localeInfo.locale.getDisplayCountry());
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
localisationPanel = new JPanel();
jLabel11 = new JLabel();
localeCombo = new JComboBox();
infoLabel = new JLabel();
currencyField = new JFormattedTextField();
jLabel1 = new JLabel();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
jLabel11.setText("Select Locale : ");
jLabel1.setHorizontalAlignment(SwingConstants.TRAILING);
jLabel1.setText("Currency : ");
GroupLayout localisationPanelLayout = new GroupLayout(localisationPanel);
localisationPanel.setLayout(localisationPanelLayout);
localisationPanelLayout.setHorizontalGroup(
localisationPanelLayout.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, localisationPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(localisationPanelLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(infoLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(Alignment.LEADING, localisationPanelLayout.createSequentialGroup()
.addGroup(localisationPanelLayout.createParallelGroup(Alignment.LEADING, false)
.addComponent(jLabel11, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(localisationPanelLayout.createParallelGroup(Alignment.LEADING)
.addComponent(currencyField)
.addComponent(localeCombo, 0, 256, Short.MAX_VALUE))))
.addContainerGap())
);
localisationPanelLayout.setVerticalGroup(
localisationPanelLayout.createParallelGroup(Alignment.LEADING)
.addGroup(localisationPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(localisationPanelLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(jLabel11)
.addComponent(localeCombo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(localisationPanelLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(currencyField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(ComponentPlacement.RELATED, 68, Short.MAX_VALUE)
.addComponent(infoLabel, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
getContentPane().add(localisationPanel, BorderLayout.CENTER);
pack();
}// </editor-fold>//GEN-END:initComponents
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new JFormattedTextFieldAndCurrency().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private JFormattedTextField currencyField;
private JLabel infoLabel;
private JLabel jLabel1;
private JLabel jLabel11;
private JComboBox localeCombo;
private JPanel localisationPanel;
// End of variables declaration//GEN-END:variables
}
If you do use setText
the display will change but the value will not get updated. 如果确实使用
setText
则显示将发生变化,但该值将不会更新。 You should use JFormattedTextField.setValue instead of setText()
, ie: 您应该使用JFormattedTextField.setValue而不是
setText()
,即:
replace: 更换:
currencyField.setText("1234567.89");
with: 有:
currencyField.setValue(1234567.89);
Here is a quote How to Use Formatted Text Fields : 这是引用如何使用格式化的文本字段 :
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.
请注意,尽管JFormattedTextField类从JTextField类继承了setText方法,但通常不会在格式化的文本字段上调用setText方法。 If you do, the field's display changes accordingly but the value is not updated (unless the field's formatter updates it constantly).
如果这样做,则字段的显示会相应更改,但是值不会更新(除非字段的格式化程序不断对其进行更新)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.