如何在JSpinner中接受字符

时间:2015-06-29 13:58:49

标签: java swing

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

2 个答案:

答案 0 :(得分:0)

enter image description here

以下示例可能有所帮助。请参阅以下代码中的评论

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

答案 1 :(得分:0)

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