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 8912783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:17:36+00:00 2026-06-15T04:17:36+00:00

I have the following code: public test() { setLayout(new FlowLayout()); JFormattedTextField price = new

  • 0

I have the following code:

public test() {
    setLayout(new FlowLayout());
    JFormattedTextField price = new JFormattedTextField(new DecimalFormat("#,##0.00 \u00A4"));
    price.setValue(new Float(105.00));
    add(price);
    add(new JButton("Ok"));

    pack();
    setVisible(true);

}

If I enter a number now, for example ’20’. The textfield enters ‘105.00’ again.
Why it doesn’t accept my entry and always goes back to the default value?

  • 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-15T04:17:38+00:00Added an answer on June 15, 2026 at 4:17 am

    there are three ways starting with easiest, ending with useless, maybe nonsense in comparing with points 1st and 2nd.

    • use NumberFormat.getCurrencyInstance(); or NumberFormat.getCurrencyInstance(Locale); for Currency symbol, then this value is valid for concrete JTextField / JFormattedTextField / JSpinner / XxxCellRenderer / XxxCellEditor,

    code

    import java.awt.GridLayout;
    import java.math.RoundingMode;
    import java.text.NumberFormat;
    import javax.swing.JFormattedTextField;
    import javax.swing.JFrame;
    import javax.swing.text.NumberFormatter;
    
    public class MaskFormatterTest {
    
        public static void main(String[] args) throws Exception {
            //NumberFormat format = NumberFormat.getNumberInstance();
            NumberFormat format = NumberFormat.getCurrencyInstance();
            format.setMaximumFractionDigits(2);
            format.setMinimumFractionDigits(2);
            format.setParseIntegerOnly(true);
            format.setRoundingMode(RoundingMode.HALF_UP);
    
            NumberFormatter formatter = new NumberFormatter(format);
            formatter.setMaximum(1000.0);
            formatter.setMinimum(0.0);
            formatter.setAllowsInvalid(false);
            formatter.setOverwriteMode(false);
    
            JFormattedTextField tf = new JFormattedTextField(formatter);
            tf.setColumns(10);
            tf.setValue(123456789.99);
            JFormattedTextField tf1 = new JFormattedTextField(formatter);
            tf1.setValue(1234567890.99);
            JFormattedTextField tf2 = new JFormattedTextField(formatter);
            tf2.setValue(1111.1111);
            JFormattedTextField tf3 = new JFormattedTextField(formatter);
            tf3.setValue(-1111.1111);
            JFormattedTextField tf4 = new JFormattedTextField(formatter);
            tf4.setValue(-56);
    
            JFrame frame = new JFrame("Test");
            frame.setLayout(new GridLayout(5, 0));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(tf);
            frame.add(tf1);
            frame.add(tf2);
            frame.add(tf3);
            frame.add(tf4);
            frame.pack();
            frame.setVisible(true);
        }
    }
    

    .

    • apply own NavigationFilter, fixed possition could be on the start or ending for possition (Bias) in JTextField / JFormattedTextField / JSpinner / XxxCellRenderer / XxxCellEditor,

    code

    //@see & read http://stackoverflow.com/questions/7421337/limited-selection-in-a-jtextfield-jtextcomponent
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class NavigationFilterPrefixWithBackspace extends NavigationFilter {
    
        private int prefixLength;
        private Action deletePrevious;
    
        public NavigationFilterPrefixWithBackspace(int prefixLength, JTextComponent component) {
            this.prefixLength = prefixLength;
            deletePrevious = component.getActionMap().get("delete-previous");
            component.getActionMap().put("delete-previous", new BackspaceAction());
            component.setCaretPosition(prefixLength);
        }
    
        @Override
        public void setDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) {
            fb.setDot(Math.max(dot, prefixLength), bias);
        }
    
        @Override
        public void moveDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) {
            fb.moveDot(Math.max(dot, prefixLength), bias);
        }
    
        class BackspaceAction extends AbstractAction {
    
            private static final long serialVersionUID = 1L;
    
            public void actionPerformed(ActionEvent e) {
                JTextComponent component = (JTextComponent) e.getSource();
                if (component.getCaretPosition() > prefixLength) {
                    deletePrevious.actionPerformed(null);
                }
            }
        }
    
        public static void main(String args[]) throws Exception {
            JTextField textField = new JTextField("Prefix_", 20);
            textField.setNavigationFilter(new NavigationFilterPrefixWithBackspace(7, textField));
            JFrame frame = new JFrame("Navigation Filter Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(textField);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    

    .

    • create own InputMask with InputVerifier, workaroung could / couldn’t be different for (each of) JTextField / JFormattedTextField / JSpinner / XxxCellRenderer / XxxCellEditor,
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code public class Test { Lock lock = new ReentrantLock();
I have following code public class TEST { public static void main(String arg[]){ try
I have the following code: public class Test extends JFrame implements ActionListener{ private static
I have the following code: class Test { public static void main(String[] args) {
I have the following simple code abstract class A { public abstract void Test(Int32
I have the following code: public class Test { public static void Main() {
I have the following code: [SetUp] public void SetMeUp() { Mapper.CreateMap<SourceObject, DestinationObject>(); } [Test]
I have the following code: public void Test(IMyInterface iInterface) { iInterface.CallMethod ( ); }
I have the following code public class Test{ private static final String key =
I have the following code: @Test public void springTest() throws SQLException{ //Connect to the

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.