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"
.
1 Answers
Answers 1
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.
0 comments:
Post a Comment