Reputation:
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. 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);
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. With my table I can not use a formatter so isEditValid is true once it gets to stopCellEditor. The only way I can see of doing this is to use an InputVerifier on the field.
The question is: Is it viable to use InputVerifiers on table cells?
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. With an InputVerifier I get access to the original field for the cell which is a subclass of JFormattedTextField with the currency info added.
I hope this makes sense.
Upvotes: 1
Views: 267
Reputation:
The problem was when validating my FormattedTextString I used getValue rather than 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.
Upvotes: 1