如何在JSpinner中禁用舍入值

时间:2015-06-26 05:39:35

标签: java swing numbers rounding jspinner

我有JSpinner,其最大分数位数为6.因此,当我输入1.1234567时,它会四舍五入为1.123457

但我的要求是当用户输入值作为自由文本时我想禁用舍入并将值设置为spinner(1.1234567)。但是,如果用户单击微调箭头按钮,则可以将其舍入。

2 个答案:

答案 0 :(得分:2)

该值通常由JSpinner的编辑器格式化。您可以更改编辑器格式化显示值的方式,这将使实际模型值保持不变

编辑器通常由JFormattedTextField支持,能够抓住它配置它会很好......

可能有几种方法可以做到这一点,但我选择使用NumberFormat因为我知道如何控制小数位数和舍入模式

首先,我们需要实现JFormattedTextField.AbstractFormatter

public class NumberFormatFormatter extends DefaultFormatter {

    private NumberFormat format;

    public NumberFormatFormatter(NumberFormat format) {
        this.format = format;
    }

    @Override
    public Object stringToValue(String string) throws ParseException {
        System.out.println("stringToValue: " + string);
        return format.parse(string);
    }

    @Override
    public String valueToString(Object value) throws ParseException {           
        String text = format.format(value);
        System.out.println("valueToString: " + value + "; " + text);
        return text;
    }

}

然后将其包装在AbstractFormatterFactory

NumberFormat numberInstance = NumberFormat.getNumberInstance();
numberInstance.setMaximumFractionDigits(6);
numberInstance.setMinimumFractionDigits(0);
numberInstance.setRoundingMode(RoundingMode.DOWN);

JFormattedTextField.AbstractFormatter format = new NumberFormatFormatter(numberInstance);
DefaultFormatterFactory dff = new DefaultFormatterFactory(format);

然后我们配置JSpinner并创建一个新的NumberEditor以满足我们的需求

JSpinner spinner = new JSpinner(new SpinnerNumberModel());
JSpinner.NumberEditor editor = new JSpinner.NumberEditor(spinner, "0.0#####");
JFormattedTextField textField = editor.getTextField();
textField.setFormatterFactory(dff);
textField.setColumns(10);
spinner.setEditor(editor);

不要忘记将编辑器应用于JSpinner;)。

这可能看起来很多(我当然这么认为),但将它包装到你自己的编辑器类的实现中并不需要花费太多时间,因为编辑器类只需要NumberFormat作为参数。

此方法将“显示”留给编辑器,但不会影响模型中的基础值,因此,如果输入1.123456789,模型仍将返回1.123456789,但它将显示1.123456

请查看How to Use Formatted Text FieldsHow to Use Spinners了解详情

Spinner

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.DefaultFormatter;
import javax.swing.text.DefaultFormatterFactory;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {

            NumberFormat numberInstance = NumberFormat.getNumberInstance();
            numberInstance.setMaximumFractionDigits(6);
            numberInstance.setMinimumFractionDigits(0);
            numberInstance.setRoundingMode(RoundingMode.DOWN);

            JFormattedTextField.AbstractFormatter format = new NumberFormatFormatter(numberInstance);
            DefaultFormatterFactory dff = new DefaultFormatterFactory(format);

            JSpinner spinner = new JSpinner(new SpinnerNumberModel());
            JSpinner.NumberEditor editor = new JSpinner.NumberEditor(spinner, "#0.######");
            JFormattedTextField textField = editor.getTextField();
            textField.setFormatterFactory(dff);
            textField.setColumns(10);
            spinner.setEditor(editor);

            setLayout(new GridBagLayout());

            spinner.setValue(1.1234567d);

            add(spinner);
        }

        public class NumberFormatFormatter extends DefaultFormatter {

            private NumberFormat format;

            public NumberFormatFormatter(NumberFormat format) {
                this.format = format;
            }

            @Override
            public Object stringToValue(String string) throws ParseException {
                System.out.println("stringToValue: " + string);
                return format.parse(string);
            }

            @Override
            public String valueToString(Object value) throws ParseException {
                String text = format.format(value);
                System.out.println("valueToString: " + value + "; " + text);
                return text;
            }

        }

    }

}

答案 1 :(得分:1)

我认为您可以做的是创建自己的JSpinner模型,类似于:

public class CustomSpinnerModel extends AbstractSpinnerModel {

    private Double value;

    public CustomSpinnerModel(Double value){
         this.value=value;
    }

    @Override
    public Object getNextValue() {
        value+=0.0000001;
        return value+"";//return as string to avoid round
    }

    @Override
    public Object getPreviousValue() {
        value-=0.0000001;
        return value+"";//return as string to avoid round
    }

    @Override
    public Object getValue() {
        return value+"";//return as string to avoid round
    }

    @Override
    public void setValue(Object object) {
        try{
           value=Double.parseDouble(object);
        } catch(Exception e){
           e.printStackTrace();
        }
    }

}