How can I know if an item in a TableView got truncated (got ellipsis)?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How can I know if an item in a TableView got truncated (got ellipsis)?



I'm using a TableView. The String data that it needs to display has variable length. How can I know if the String data was too long (and hence got ellipses)?



My guess is to use a custom TableCell (calling setCellFactory()), and in the updateItem() I should query the pixel-width of the data, but I don't know how.


column.setCellFactory(column ->
new TableCell<Transaction, String>()
@Override
protected void updateItem(final String item, final boolean empty)
super.updateItem(item, empty);
setText(item);


if (/*measuredWidth > getWidth()*/)
setTooltip(new Tooltip(item));



);



Any ideas on this?





I'm not sure, but I know for a label you do this: label.setMinWidth(Region.USE_PREF_SIZE); Maybe it's similar...
– trilogy
Sep 14 at 1:43



label.setMinWidth(Region.USE_PREF_SIZE);





you can query the cells prefWidth and compare it to the column width (guessing, didn't try ;)
– kleopatra
Sep 14 at 8:05





Did you figure this one out? I'd like to do this too...
– trilogy
14 hours ago





Not yet. I added tooltip for all items regardless of what their width was. For now.
– Miklos Jakab
13 hours ago





Ok figured it out...
– trilogy
17 mins ago




1 Answer
1


column.setCellFactory(col -> new TableCell<Object, String>()

@Override
protected void updateItem(final String item, final boolean empty)

super.updateItem(item, empty);
setText(item);
TableColumn tableCol = (TableColumn) col;

if (item != null)

//I don't know why or if this is scalable, but -10 worked on the edge cases
if (tableCol.getWidth()-10 < new Text(item).getLayoutBounds().getWidth())

setTooltip(new Tooltip(item));



);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered