I'm trying to "smartly" abbreviate/truncate the String
contents of a column nested in a TableView. I have tried using UNCONSTRAINED_RESIZE_POLICY
but really prefer CONSTRAINED_RESIZE_POLICY
since it auto-resizes the columns based on the window size.
What I'm trying to accomplish:
String one = "SomeVeryLongString";
// Displayed in the column where user has shrunk it too narrow
+-------------+
| Some...ring |
+-------------+
// and if the user shrunk it a bit more
+-----------+
| Som...ing |
+-----------+
// and if they stretch it a bit
+---------------+
| SomeV...tring |
+---------------+
// etc...
I have explored the possibility of reading the String
length and doing my own truncating, but then it's not possible to have this dynamically update as the user shrinks/stretches the gui. My gut says this must be done with the JavaFX Classes
since it would be closely tied with TableView
, however I have not found a way.
How can I accomplish this using JavaFX
?
Solution
The default overrun behavior of a standard TableCell is to truncate it's string and display
...
at the end of the string to indicate that the string has been truncated. All Labeled controls in JavaFX work this way.The examples in your question have
...
ellipsises in the middle of the strings. To achieve that, set the overrun style on the cell generated by the appropriate cell factory:The text string displayed for ellipsises can also be modifed by setting a new ellipsis string on the cell generated by the appropriate cell factory:
A simple table cell which demonstrates this is:
Sample Application
The
CenteredOverrunTableCell
is used in the following sample application.<--->
...
ellipsis at the end of the overrun string.