繁体   English   中英

如何在JSpinner中接受字符

[英]How to accept characters in JSpinner

我有将输入1M转换为1000000的文档。如果我将文档设置为JTextfield,则可以正常工作。 但是,当我将此设置为JSpinner的文本字段时。 它不起作用,因为微调器在内部拒绝无效的值,例如“ m”。 因此,如果我键入1m,则只有1个作为输入字符串发送到文档。 任何帮助将不胜感激。

在此处输入图片说明

以下示例可能会有所帮助。 请查看以下代码中的注释

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.JTextField;

public class jspinnerdemo {

    public static void main(String[] args) {
        JFrame f= new JFrame("JSpinner Demo");
        f.setSize(500,100);
        f.setLayout(new GridLayout(1, 3));
        JSpinner ctrlSpin = new JSpinner();
        //Below code will get the text field (JFormattedTextField) component 
        //from the jspinner and set its format to null
        ((JSpinner.NumberEditor)ctrlSpin.getComponent(2)).getTextField().setFormatterFactory(null);
        JButton ctrlButton = new JButton("Click");
        JTextField ctrlTxt = new JTextField("");
        f.add(ctrlSpin);
        f.add(ctrlButton);
        f.add(ctrlTxt);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        ctrlButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //So when get the text from the textfield it will return the text as what typed
                ctrlTxt.setText(""+((JSpinner.NumberEditor)ctrlSpin.getComponent(2)).getTextField().getText());
            }
        });
    }
}

JSpinner.NumberEditor需要一个DecimalFormat,因此您必须提供一个自定义的DecimalFormat:

JSpinner spinner = new JSpinner(new SpinnerNumberModel());

final Map<String, Long> multipliers = new HashMap<>();
multipliers.put("K", 1_000L);
multipliers.put("M", 1_000_000L);
multipliers.put("G", 1_000_000_000L);
multipliers.put("T", 1_000_000_000_000L);

DecimalFormat letterFormat = new DecimalFormat(",##0") {
    @Override
    public Number parse(String text,
                        ParsePosition pos) {
        int len = text.length();
        if (len > 0) {
            String lastChar = text.substring(len - 1, len);
            lastChar = lastChar.toUpperCase(Locale.US);
            Long multiplier = multipliers.get(lastChar);
            if (multiplier != null) {
                Number value =
                    super.parse(text.substring(0, len - 1), pos);
                if (value == null) {
                    return null;
                }

                // Skip past multiplier char.
                pos.setIndex(pos.getIndex() + lastChar.length());

                if (value instanceof BigDecimal) {
                    return ((BigDecimal) value).multiply(
                        BigDecimal.valueOf(multiplier));
                } else if (value instanceof BigInteger) {
                    return ((BigInteger) value).multiply(
                        BigInteger.valueOf(multiplier));
                } else if (value instanceof Double ||
                           value instanceof Float) {
                    return value.doubleValue() * multiplier;
                } else {
                    return value.longValue() * multiplier;
                }
            }
        }

        return super.parse(text, pos);
    }
};

JSpinner.DefaultEditor editor =
    (JSpinner.DefaultEditor) spinner.getEditor();
NumberFormatter formatter = (NumberFormatter)
    editor.getTextField().getFormatter();
formatter.setFormat(letterFormat);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM