【问题标题】:JSpinner getting null valueJSpinner获取空值
【发布时间】:2013-05-15 12:11:54
【问题描述】:

当我的JSpinner 为空时,我想取空值。但是当我调用getValue() 函数时,它会返回0.0

  public JFormattedTextField getTextField(JSpinner spinner) {
    JComponent editor = spinner.getEditor();
    if (editor instanceof JSpinner.DefaultEditor) {
        return ((JSpinner.DefaultEditor)editor).getTextField();
    } else {
        return null;
    }
}

这是我的方法调用:

Object a = getTextField(jValueNum).getValue();

编辑:或者有什么办法可以超载 getValue() 函数?

【问题讨论】:

    标签: java swing isnull jspinner jformattedtextfield


    【解决方案1】:

    @user2279774:使用用户定义的值设置 jSpinner。这将是我唯一知道的方式。例如:

    String[] colors = new String[] {
                    "Red", "Orange", "Yellow", "Green", "Blue","  ", "Purple"
            };
            SpinnerListModel model = new SpinnerListModel(colors);
    

    【讨论】:

      【解决方案2】:

      但是当我调用 getValue() 函数时它返回 0.0。

      如果没有对带有 Number Formatter 的 JFormattedTextField 的脏破解,这是不可能的,无论 Number 实例中的任何数字都有默认值

      当我的 jSpinner 为空时,我想取 null 值

      在JSpinner返回double.valueOf(0.0)的情况下返回null(错误的想法,因为数字应该用于计算)

      对象 a = getTextField(jValueNum).getValue();

      JSpinner 也可以,没有理由使用派生编辑器返回

      【讨论】:

        【解决方案3】:

        检查您设置的微调器型号。可能你已经设置了 SpinnerNumberModel。尝试使用 SpinnerDataModel 并以字符串格式设置所有值。

        它将返回为对象配置的编辑器(JFormattedTextField),您将获得 null 作为值。 :)

        【讨论】:

        • 我需要一个 SpinnerNumberModel。用户应输入一个数字。但是对于没有值的变量,用户应该能够将其留空以显示空值。
        • see hack 对于 JFormattedTextField,将此代码应用于 JSpinnrs 编辑器组件
        • 不是问题...,使用编辑器应用 Documentlistener 或 InputVerifier 并仅接受来自用户的数字数据,但在内部保留为微调器模型中的字符串。 :)
        • 什么是SpinnerDataModel
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-20
        • 1970-01-01
        • 2021-04-19
        • 2019-08-27
        • 1970-01-01
        相关资源
        最近更新 更多