简体   繁体   English

输入验证器单元格编辑器

[英]Inputverifier cell editor

I have a table with a currency amount in one column. 我有一个表格,其中一列中包含货币金额。 The difficulty is that each row can have a different currency. 困难在于每一行可以有不同的货币。 I have set up a default cell editor which does the validaion in stopCellEditing which works fine. 我已经设置了一个默认的单元格编辑器,该编辑器在stopCellEditing中执行验证,效果很好。 If the data is invalid the editor does not stop but the value is reverted to its original value rather than leave the invalid data in the cell. 如果数据无效,则编辑器不会停止,而是将值恢复为其原始值,而不是将无效数据保留在单元格中。 I have set the Focus Lost behaviour: 我已经设定了失去焦点的行为:

ftf.setFocusLostBehavior(JFormattedTextField.PERSIST); ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);

I have looked at the example for cell editors (see Specifying Formatters and Using Formatter Factories ) and it works because it uses an integer format: 我看了单元格编辑器的示例(请参阅“ 指定格式化程序”和“使用格式化程序工厂” ),它可以工作,因为它使用整数格式:

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

This format factory sets the isEditValid=false within the JFormattedTextField so when stopCellEditing is called it has already set the isEditValid to false. 此格式工厂在JFormattedTextField中设置了isEditValid = false,因此在调用stopCellEditing时,它已经将isEditValid设置为false。 With my table I can not use a formatter so isEditValid is true once it gets to stopCellEditor. 对于我的表,我不能使用格式化程序,因此一旦它进入stopCellEditor,isEditValid为true。 The only way I can see of doing this is to use an InputVerifier on the field. 我看到的唯一方法是在字段上使用InputVerifier。

The question is: Is it viable to use InputVerifiers on table cells? 问题是:在表格单元格上使用InputVerifiers是否可行?

I have looked to overriding stringToValue and valueToString in a Formatter but I do not have access to the details of the currency of the row, just the string. 我曾试图在格式化程序中覆盖stringToValue和valueToString,但是我无法访问该行的货币的详细信息,而只能访问字符串。 With an InputVerifier I get access to the original field for the cell which is a subclass of JFormattedTextField with the currency info added. 使用InputVerifier,我可以访问单元格的原始字段,该单元格是JFormattedTextField的子类,并添加了货币信息。

I hope this makes sense. 我希望这是有道理的。

The problem was when validating my FormattedTextString I used getValue rather than getText. 问题是验证我的FormattedTextString时,我使用的是getValue而不是getText。 Value is set to the original value before entering the new data. 在输入新数据之前,将值设置为原始值。 Text is set to the new value. 文本设置为新值。 Once this was changed it acted as expected. 一旦更改,它就会按预期运行。

For future reference InputVerifier on a FormattedTextField within a table is not called until the cell processing has occurred so it can not be used for table cell validation. 供将来参考,在单元格处理发生之前,将不调用表中FormattedTextField上的InputVerifier,因此不能将其用于表单元格验证。

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

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