0

javaFX:TableView 的单元格值不足以显示在列中,它会被截断'...' 的末尾,如何在这样的单元格上显示提示?我需要一个小费,因为有时我无法拖动柱子的头部。所以我看不到单元格的全部内容。

4

2 回答 2

2

下面的 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);
    }
}

结果将如下所示:

带有工具提示的单元格

于 2013-03-30T09:06:27.797 回答
0

这实际上应该可以解决您的问题。

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

        }
    });
于 2018-09-20T20:43:02.657 回答