首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >javaFX: TableView的单元格值不足以显示在列中,它将被裁剪,如何在这样的单元格上显示提示?

javaFX: TableView的单元格值不足以显示在列中,它将被裁剪,如何在这样的单元格上显示提示?
EN

Stack Overflow用户
提问于 2013-03-28 22:03:43
回答 2查看 1.6K关注 0票数 0

javaFX: TableView的单元格值不足以显示在列中,它将被裁剪到“...”的末尾,如何在这样的单元格上显示提示?我需要小费,因为有时候我拖不动柱子的头。所以我看不到单元格的全部内容。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-30 17:06:28

下面,TableCell的子类用于设置单元格的ToolTip。它不区分适合文本的单元格和文本太大的单元格。它在单元格工厂中用于表列。

import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;

public class SO extends Application {
    static class XCell extends TableCell<String, String> {
        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            this.setText(item);
            this.setTooltip(
                    (empty || item==null) ? null : new Tooltip(item));
        }
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        StackPane pane = new StackPane();
        Scene scene = new Scene(pane, 300, 100);
        primaryStage.setScene(scene);
        ObservableList<String> list = FXCollections.observableArrayList(
                "Short",
                "This is quite long - almost very long");
        TableView<String> tv = new TableView<>(list);
        TableColumn<String, String> col = new TableColumn<>("Column");
        col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<String,String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(CellDataFeatures<String, String> param) {
                return new ReadOnlyStringWrapper(param.getValue());
            }
        });
        col.setMaxWidth(50);
        col.setCellFactory(new Callback<TableColumn<String,String>, TableCell<String,String>>() {
            @Override
            public TableCell<String, String> call(TableColumn<String, String> param) {
                return new XCell();
            }
        });
        tv.getColumns().add(col);
        pane.getChildren().add(tv);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

结果将如下所示:

票数 2
EN

Stack Overflow用户

发布于 2018-09-21 04:43:03

这实际上应该解决了你的问题。

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

        }
    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15683907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档