Frage

I have JFormattedTextFields in my application to show some cash values.

Initially all the fields are set to 0.00f;

But, the first component, which has the focus shows the value as ".00". Others display their values as expected.

How should i make the display value as "0.00" when the formatted textfield has the focus.

Following is Mmy sample code.

import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;


public class MainFrame extends JFrame {

    public MainFrame()
    {
    setSize(300, 300);
    }
    /**
     * @param args
     */
    public static void main(String[] args) 
    {
    final MainFrame frame = new MainFrame();
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(3, 1));

    ExtedndedFormattedTextField tf = new ExtedndedFormattedTextField();
    tf.setAmount(0.00f);

    ExtedndedFormattedTextField tf1 = new ExtedndedFormattedTextField();
    tf1.setAmount(0.00f);

    ExtedndedFormattedTextField tf2 = new ExtedndedFormattedTextField();
    tf2.setAmount(0.00f);

        tf.setColumns(20);
        panel.add(tf);
        panel.add(tf1);
        panel.add(tf2);

        frame.add(panel);
        frame.pack();
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
        frame.setVisible(true);

        }
    });
    }

}

    class ExtedndedFormattedTextField extends JFormattedTextField
    {
        private String negativeSymbol = "";
        public ExtedndedFormattedTextField()
        {
        super();
        setFormatterFactory(getCurrencyFormaterFactory());
        this.addFocusListener(new FocusListener() {

            @Override
            public void focusLost(FocusEvent e) {


            }

            @Override
            public void focusGained(FocusEvent e) {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                select(0, getText().length());

                }
            });


            }
        });
        }

        private AbstractFormatterFactory getCurrencyFormaterFactory()
        {
            NumberFormatter displayFormatter = new NumberFormatter(DecimalFormat.getCurrencyInstance());
            DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols();
            negativeSymbol = String.valueOf(formatSymbols.getMinusSign());
            NumberFormatter editFormatter = new NumberFormatter(new DecimalFormat("#.00",
                formatSymbols));
            editFormatter.setAllowsInvalid(false);
            editFormatter.setOverwriteMode(false);
            return new DefaultFormatterFactory(displayFormatter, displayFormatter, editFormatter);
        }

        public float getAmount()
        {
            try
            {
              commitEdit();
            }
            catch (ParseException e)
            {
              return 0;
            }

            Object o = getValue();
            if (o instanceof Number)
            {
                return ((Number) getValue()).floatValue();
            }
            return 0;
        }

        public void setAmount(float amount)
        {
            setValue(new Float(amount));
        }

        @Override
        public boolean requestFocusInWindow() {
        this.select(0, getText().length()); 
        return super.requestFocusInWindow();
        }


    }
War es hilfreich?

Lösung

If you use # character in formatter, it will show zeros as absent. Use 0, so it will not skip them. So change:

NumberFormatter editFormatter = new NumberFormatter(new DecimalFormat("#.00", formatSymbols));

to

NumberFormatter editFormatter = new NumberFormatter(new DecimalFormat("0.00", formatSymbols));

Andere Tipps

The easiest way is to initialize the field with a Format:

tf = new JFormattedTextField(NumberFormat.getCurrencyInstance());

If you just want the number, and don't want to see the currency symbol:

tf = new JFormattedTextField(new DecimalFormat("0.00"));

Since you've written a subclass (thought I suspect subclassing is not necessary), you can make this the first line of your constructor:

super(NumberFormat.getCurrencyInstance());

Or:

super(new DecimalFormat("0.00"));
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top