Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8501355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:11:24+00:00 2026-06-11T01:11:24+00:00

i have used the information in the below link to display the currency format

  • 0

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?

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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:11:25+00:00Added an answer on June 11, 2026 at 1:11 am

    If you do use setText the display will change but the value will not get updated. You should use JFormattedTextField.setValue instead of setText(), ie:

    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. If you do, the field’s display
    changes accordingly but the value is not updated (unless the field’s
    formatter updates it constantly).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have developed application for read information from card reader. Here i have used
I have a dynamic list of items that will be used to POST information
I have used the silverlight control in CRM 2011.It also published on form but
I have used a plugin that uses prototype js, it's working fine but the
I have used NSSets many times in my apps, but I have never created
I have used two frame in my web page... the below frame contain the
HUMAN ERROR. THE CODE BELOW DOES NOT HAVE ERROR Moderators: Please remove this. If
Thanks for the answers, I have not used StackOverflow before so I was suprised
I have looked throughout the greatest-n-per-group tag and found great information but nothing that
I have always used the Scriptmanager to handle all AJAX calls, but I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.