1

我有一张表,其中有一列中的货币金额。困难在于每一行可以有不同的货币。我已经设置了一个默认的单元格编辑器,它在 stopCellEditing 中进行验证,效果很好。如果数据无效,编辑器不会停止,但会将值恢复为其原始值,而不是将无效数据留在单元格中。我已经设置了 Focus Lost 行为:

ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);

我查看了单元格编辑器的示例(请参阅指定格式化程序和使用格式化程序工厂),它之所以有效,是因为它使用整数格式:

       //Set up the editor for the integer cells.
    integerFormat = NumberFormat.getIntegerInstance();
    NumberFormatter intFormatter = new NumberFormatter(integerFormat);
    intFormatter.setFormat(integerFormat);
    intFormatter.setMinimum(minimum);
    intFormatter.setMaximum(maximum);

    ftf.setFormatterFactory(
            new DefaultFormatterFactory(intFormatter));
    ftf.setValue(minimum);
    ftf.setHorizontalAlignment(JTextField.TRAILING);
    ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);

此格式工厂在 JFormattedTextField 中设置 isEditValid=false,因此当调用 stopCellEditing 时,它已经将 isEditValid 设置为 false。对于我的表,我不能使用格式化程序,因此一旦它到达 stopCellEditor,isEditValid 就为真。我能看到的唯一方法是在现场使用 InputVerifier。

问题是:在表格单元格上使用 InputVerifiers 是否可行?

我已经考虑在 Formatter 中覆盖 stringToValue 和 valueToString,但我无法访问该行货币的详细信息,只能访问字符串。使用 InputVerifier,我可以访问单元格的原始字段,该单元格是 JFormattedTextField 的子类,并添加了货币信息。

我希望这是有道理的。

4

1 回答 1

1

问题是在验证我的 FormattedTextString 时,我使用了 getValue 而不是 getText。在输入新数据之前将值设置为原始值。文本设置为新值。一旦改变了它,它就会按预期运行。

对于将来的参考,在单元格处理发生之前不会调用表格中 FormattedTextField 上的 InputVerifier,因此它不能用于表格单元格验证。

于 2014-09-16T06:51:10.963 回答