如何仅在文本被椭圆化的那些表格单元格上显示工具提示(...)? [英] How to show tooltip only on those table cells which text is ellipsized (...)?

查看:121
本文介绍了如何仅在文本被椭圆化的那些表格单元格上显示工具提示(...)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要创建工具提示,请根据此答案使用以下代码。

To create tooltip I use the following code based on this answer.

Callback<TableColumn,TableCell> existingCellFactory   = column.getCellFactory();
column.setCellFactory(c -> {
    TableCell cell = existingCellFactory.call((TableColumn) c);
    Tooltip tooltip = new Tooltip();
    tooltip.textProperty().bind(cell.textProperty());
    cell.tooltipProperty().bind(
            Bindings.when(Bindings.or(cell.e‌mptyProperty(), cell.itemProperty().isNull()))
                    .then((Tooltip) null).otherwise(tooltip));
    return cell ;
});

问题在于,在这种情况下,当用户在桌面上移动鼠标时,任何单元格上出现工具提示和用户得到太多的工具提示。

The problem is that in this case when user moves mouse on table on any cell tooltip appears there and user gets too many tooltips.

如何仅在那些表格单元格上显示工具提示文本是更宽的单元格宽度并在末尾替换为...?

How to show tooltip only on those table cells which text is wider cell width and replaced to "..." at the end?

推荐答案

这可以解决您的问题。

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 && tableCol.getWidth() < new Text(item + "  ").getLayoutBounds().getWidth())
        {
            tooltipProperty().bind(Bindings.when(Bindings.or(emptyProperty(), itemProperty().isNull())).then((Tooltip) null).otherwise(new Tooltip(item)));
        } else
        {
            tooltipProperty().bind(Bindings.when(Bindings.or(emptyProperty(), itemProperty().isNull())).then((Tooltip) null).otherwise((Tooltip) null));
        }

    }
});

这篇关于如何仅在文本被椭圆化的那些表格单元格上显示工具提示(...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆