Reputation: 2209
So in my javafx program I need to display dates, both as a Label in forms, as well as cells in TableViews.
However I don't want the long date format "14 Sept 2014.." etc. I want it to display merely as "3 days ago" "2 min ago" or perhaps just 3d or 2m. Let's call this the elapsed format.
However, it's not good enough in a TableCell to create such a string, like Facebook and other pages, 2m should update 3m on minute intervals without user intervention. Same goes for hours/days etc.
If I am writing from scratch....
I imagine that I'd have to launch a thread that is connects to all these widgets and polls into these widgets (perhaps using Platform.runLater).
It seems I need my own derived Label as it needs to be able to accommodate a .setDate(Date).
Ideally, the cherry on the top would be if both the columns and the labels could switch between the long format and the elapsed format on a right click perhaps.
In my mind the user should not have to do anything more than instantiate these widgets, they should connect themselves up in the background.
However, this is sufficiently complex that I just wanted to ask if something like this doesn't already exist? Or am I thinking about this the wrong way.
Upvotes: 3
Views: 698
Reputation: 159576
Take a look at the code in the digital clock label answer to Displaying changing values in JavaFx Label.
Apply a similar concept, but have a cell factory generate your "clock labels". And rather than having your clock labels report the current time, have them report elapsed time from their instantiation. The internal animation timelines inherent in each instantiated clock label make them self updating, so in the triggered key frame you can just set the label to whatever you want.
Upvotes: 2