I am using Java 8, after following documentation:
I'd like to setup a specialized formatter when editing a column in my JTable. This column contains java.time.LocalTime instances.
JTable table;
...
table.setDefaultEditor(LocalTime.class, new LocalTimeEditor());
Where LocalTimeEditor is defined by (tentatively):
public class LocalTimeEditor extends DefaultCellEditor {
    JFormattedTextField ftf;
  public LocalTimeEditor() {
    super(new JFormattedTextField());
    ftf = (JFormattedTextField) getComponent();
    // Set up the editor for the LocalTime cells.
    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
    ftf.setFormatterFactory(new DefaultFormatterFactory(dateFormatter));
But this leads to the following compilation error:
The constructor DefaultFormatterFactory(DateTimeFormatter) is undefined
I'd like to stay away from a solution involving SimpleDateFormat (+DateFormatter) as explained here or here, since java.util.Date should be considered legacy (see old code here).
Is there a solution to integrate DateTimeFormatter with JFormattedTextField, or am I blocked by:
I'd also like to stay away from MaskFormatter, since it does not allow easy error handling for something like: "25:70:90".
JTable(): A table is created with empty cells. JTable(int rows, int cols): Creates a table of size rows * cols. JTable(Object[][] data, Object []Column): A table is created with the specified name where []Column defines the column names.
As per the argument of DefaultFormatterFactor, I created a new JFormattedTextField.AbstractFormatter 
class JTFormater extends JFormattedTextField.AbstractFormatter{
    final DateTimeFormatter formatter;
    public JTFormater(DateTimeFormatter formatter){
        this.formatter =  formatter;
    }
    @Override
    public Object stringToValue(String text) throws ParseException {
        return formatter.parse(text);
    }
    @Override
    public String valueToString(Object value) throws ParseException {
        if(value instanceof TemporalAccessor){
            return formatter.format((TemporalAccessor) value);
        } else{
            throw new ParseException("not a valid type at", 0);
        }
    }
}
From this I could parse and display LocalTime's, although in my implementation it is pretty clumsy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With