3

This is my code:

// create the formatters, default, display, edit
NumberFormatter defaultFormatter = new NumberFormatter(new DecimalFormat("#.##"));
NumberFormatter displayFormatter = new NumberFormatter(new DecimalFormat("#.##€"));
NumberFormatter editFormatter = new NumberFormatter(new DecimalFormat("#.##")); 
// set their value classes
defaultFormatter.setValueClass(Double.class);
displayFormatter.setValueClass(Double.class);
editFormatter.setValueClass(Double.class);   
// create and set the DefaultFormatterFactory
DefaultFormatterFactory valueFactory = new DefaultFormatterFactory(defaultFormatter,displayFormatter,editFormatter);
jFormattedTextField4.setFormatterFactory(valueFactory);
jFormattedTextField1.setFormatterFactory(valueFactory);

I'm getting issues when the user input a value with a point, like (2.33) , the formatedtextfield change to (2€) and this is not suppose to, because the user entered a valid value. If the user input (2,33) the result is (2,33) and again is not suppose to, because should be (2.33)

Basically i always want the format (X.XX). Example:

input vs what i want: 2-2.00€ / 2,22-2.22€ / 312.54-312.54€ / 432-432.00€ / 2,2-2.20€

EDITED: one week ago this was working, after i mount this project in another windows the issue appear. At this time i have a portuguese keyboard. Is it possible that the issue is involved with any windows or system property?

Edited:I still looking for help. Anyone??? :s

1 Answers1

1
  • you have to use getCurrencyInstance(), instead of plain vanilla DecimalFormat, be sure something went wrong in your description ("one week ago this was working, after i mount this project in another windows the issue appear....")

  • and/or there you can to define Formatter restricted by the Locale

  • I wouldn't be to use CurrencyInstance, isn't nice for me, there is possible to force real shorthand name for (e.g. Euro = EUR) by using NavigationFilter and chars position in row with setDot/moveDot, code example by @camickr

  • here are simple standards, defaults thats you required,

enter image description here

import java.awt.*;
import java.awt.font.TextAttribute;
import java.math.*;
import java.text.*;
import java.util.Map;
import javax.swing.*;
import javax.swing.JFormattedTextField.*;
import javax.swing.event.*;
import javax.swing.text.InternationalFormatter;

public class DocumentListenerAdapter {

    public DocumentListenerAdapter() {
        JFrame frame = new JFrame("AbstractTextField Test");
        final JFormattedTextField textField1 = new JFormattedTextField(new Double(10.01));
        textField1.setFormatterFactory(new AbstractFormatterFactory() {
            @Override
            public AbstractFormatter getFormatter(JFormattedTextField tf) {
                NumberFormat format = DecimalFormat.getCurrencyInstance();
                format.setMinimumFractionDigits(2);
                format.setMaximumFractionDigits(2);
                format.setRoundingMode(RoundingMode.HALF_UP);
                InternationalFormatter formatter = new InternationalFormatter(format);
                formatter.setAllowsInvalid(false);
                //formatter.setMinimum(0.0);
                //formatter.setMaximum(1000.00);
                return formatter;
            }
        });
        final Map attributes = (new Font("Serif", Font.BOLD, 16)).getAttributes();
        attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
        final JFormattedTextField textField2 = new JFormattedTextField(new Double(10.01));
        textField2.setFormatterFactory(new AbstractFormatterFactory() {
            @Override
            public AbstractFormatter getFormatter(JFormattedTextField tf) {
                NumberFormat format = DecimalFormat.getInstance();
                format.setMinimumFractionDigits(3);
                format.setMaximumFractionDigits(3);
                format.setRoundingMode(RoundingMode.HALF_UP);
                InternationalFormatter formatter = new InternationalFormatter(format);
                formatter.setAllowsInvalid(false);
                //formatter.setMinimum(0.0);
                //formatter.setMaximum(1000.00);
                return formatter;
            }
        });
        textField2.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void changedUpdate(DocumentEvent documentEvent) {
                printIt(documentEvent);
            }

            @Override
            public void insertUpdate(DocumentEvent documentEvent) {
                printIt(documentEvent);
            }

            @Override
            public void removeUpdate(DocumentEvent documentEvent) {
                printIt(documentEvent);
            }

            private void printIt(DocumentEvent documentEvent) {
                DocumentEvent.EventType type = documentEvent.getType();
                double t1a1 = (((Number) textField2.getValue()).doubleValue());
                if (t1a1 > 1000) {
                    Runnable doRun = new Runnable() {
                        @Override
                        public void run() {
                            textField2.setFont(new Font(attributes));
                            textField2.setForeground(Color.red);
                        }
                    };
                    SwingUtilities.invokeLater(doRun);
                } else {
                    Runnable doRun = new Runnable() {
                        @Override
                        public void run() {
                            textField2.setFont(new Font("Serif", Font.BOLD, 16));
                            textField2.setForeground(Color.black);
                        }
                    };
                    SwingUtilities.invokeLater(doRun);
                }
            }
        });
        //https://stackoverflow.com/a/20008786/714968
        JFormattedTextField jftf2 = new JFormattedTextField();
        final InternationalFormatter fmt = new InternationalFormatter(
                new MessageFormat("{0,number,000}-{1,number,0000}"));
        jftf2.setFormatterFactory(new JFormattedTextField.AbstractFormatterFactory() {
            @Override
            public JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField tf) {
                return fmt;
            }
        });
        jftf2.setValue(new Object[]{111, 1234});
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(textField1, BorderLayout.NORTH);
        frame.add(textField2, BorderLayout.CENTER);
        frame.add(jftf2, BorderLayout.SOUTH);
        frame.setVisible(true);
        frame.pack();
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                DocumentListenerAdapter main = new DocumentListenerAdapter();
            }
        });
    }
}
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319