如何在JFormattedTextFiled具有焦点时设置0.00

时间:2013-11-13 11:59:05

标签: java swing jformattedtextfield

我的应用程序中有JFormattedTextFields来显示一些现金值。

最初所有字段都设置为0.00f;

但是,第一个具有焦点的组件将值显示为“.00”。其他人按预期显示其值。

当格​​式化文本字段具有焦点时,如何将显示值设为“0.00”。

以下是Mmy示例代码。

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();
        }


    }

2 个答案:

答案 0 :(得分:0)

最简单的方法是使用Format:

初始化字段
tf = new JFormattedTextField(NumberFormat.getCurrencyInstance());

如果您只想要号码,并且不想看到货币符号:

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

既然你已经编写了一个子类(我以为我怀疑子类是不必要的),你可以将它作为构造函数的第一行:

super(NumberFormat.getCurrencyInstance());

或者:

super(new DecimalFormat("0.00"));

答案 1 :(得分:0)

如果在格式化程序中使用#colracter,则会显示零缺失。使用0,所以不会跳过它们。所以改变:

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

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