1

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.

  • Trying it out the inputverifier gets called after stopCellEditing by the super(StopCellEditing()). I need to validate the field prior to this. –  Sep 15 '14 at 09:22
  • Posible [duplicate](http://stackoverflow.com/q/7531513/230513). – trashgod Sep 15 '14 at 13:23
  • Not quite, this one is a bit deeper. The table cell is defined as a string so there is no real error checking whilst the user is typing. When focus is lost and the stopCellEditor method is called the flag isEditValid is set to true. I do the validation I require in this method and return false when there is an error. The cell is then REVERTED rather than PERSISTED as I have asked for. What I am asking is there a way of intercepting the editing of the cell so I can set isEditValid to false? I could write a custom formatter but that gets called for every key pressed. Any suggestions? –  Sep 16 '14 at 05:46

1 Answers1

1

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.