javaFX:TableView的cellvalue不足以在列中显示,它会被剪裁,如何在这样的单元格上显示提示? [英] javaFX: TableView's cellvalue is not enough to display in columns, it will be clipped, how to show a tip on such cell?

查看:140
本文介绍了javaFX:TableView的cellvalue不足以在列中显示,它会被剪裁,如何在这样的单元格上显示提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javaFX:TableView的cellvalue不足以在列中显示,它将被剪切到'...'结尾,如何在这样的单元格上显示提示?
我需要小费,因为有时我无法拖动colunm的脑袋。所以我看不到单元格的全部内容。

javaFX: TableView's cellvalue is not enough to display in columns, it will be clipped end of '...', how to show a tip on such cell? I need a tip because sometimes i can't drag the colunm's head. So i can't see the whole content of the cell.

推荐答案

在TableCell的子类下面用于设置工具提示一个细胞。它不区分文本适合的单元格和文本太大的单元格。它在单元格工厂中用于表格列。

Below a subclass of TableCell is used to set the ToolTip for a cell. It does not distinguish between cells in which the text fits and those where the text is too large. It is used in the cell factory for the table column.

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

结果将如下所示:

这篇关于javaFX:TableView的cellvalue不足以在列中显示,它会被剪裁,如何在这样的单元格上显示提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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