代码之家  ›  专栏  ›  技术社区  ›  Miklos Jakab

如何知道TableView中的项是否被截断(省略)?

  •  2
  • Miklos Jakab  · 技术社区  · 7 年前

    我的猜测是使用一个定制的TableCell(调用setCellFactory()),在updateItem()中我应该查询数据的像素宽度,但我不知道如何查询。

            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));
                        }
                    }
                }
        );
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   trilogy    7 年前
    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));
                }
    
            }
        });
    
    推荐文章