繁体   English   中英

输入验证器单元格编辑器

[英]Inputverifier cell editor

我有一个表格,其中一列中包含货币金额。 困难在于每一行可以有不同的货币。 我已经设置了一个默认的单元格编辑器,该编辑器在stopCellEditing中执行验证,效果很好。 如果数据无效,则编辑器不会停止,而是将值恢复为其原始值,而不是将无效数据保留在单元格中。 我已经设定了失去焦点的行为:

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为true。 我看到的唯一方法是在字段上使用InputVerifier。

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

我曾试图在格式化程序中覆盖stringToValue和valueToString,但是我无法访问该行的货币的详细信息,而只能访问字符串。 使用InputVerifier,我可以访问单元格的原始字段,该单元格是JFormattedTextField的子类,并添加了货币信息。

我希望这是有道理的。

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

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

暂无
暂无

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

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