无法在JavaFX TableView单元格值上显示工具提示

Ash*_*oli 5 java javafx-2

在我的JavaFX TableView我有一个TableColumn我已设置细胞工厂渲染ProgressBar和其他TableColumn我已设置细胞工厂显示ToolTip.如下图所示.第二列显示进度条,其他3列呈现以显示工具提示,其中包含要显示的简单字符串值.

在此输入图像描述

我遇到的问题TableView是没有在表中显示/显示更新的值,即UI没有验证/刷新/绘制TableView元素.如果我点击ColumnHeader任何列进行排序,那么我只能看到TableView更新.手动排序表列以刷新表内容没有意义,所以我搜索并找到解决方案来显示/隐藏表列以更新表视图.

为了解决这个问题,我在下面编写了一个代码来解决TableView更新/刷新问题,但由于此代码现在ToolTip无法显示.

每个特定时间间隔后更新表视图的代码

 class TableProgressBarUpdator implements Runnable {

        TableView table;

        public TableProgressBarUpdator(TableView fxtable) {
            table = fxtable;

        }

        public void start() {
            new Thread(this).start();
        }

        public void run() {

            while (keepUpdating) {
                try {
                    updateProgressbar();
                    Thread.sleep(1000);
                } catch (Exception e) {
                    LogHandler.doErrorLogging("Error while updating tables cell", e);
                }
            }
            LogHandler.doDebugLogging("Table process repainting is completed.");
        }

        private void updateProgressbar() throws Exception {
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    ((TableColumn) table.getColumns().get(0)).setVisible(false);
                    ((TableColumn) table.getColumns().get(0)).setVisible(true);
                }
            });


        }
    }
Run Code Online (Sandbox Code Playgroud)

开始更新表视图

public void startUpdatingTableProgress() {
    keepUpdating = true;
    TableProgressBarUpdator tpu = new TableProgressBarUpdator(table);
    tpu.start();
}
Run Code Online (Sandbox Code Playgroud)

停止更新表视图

public void stopUpdatingTableProgress() {
        keepUpdating = false;
    }
Run Code Online (Sandbox Code Playgroud)

添加更多显示渲染类的代码以显示进度条和显示工具提示.

显示进度条表视图的代码.

public static class ProgressBarTableCell<S, T> extends TableCell<S, T> {

        private final ProgressBar progressBar;
        private ObservableValue<T> ov;

        public ProgressBarTableCell() {
            this.progressBar = new ProgressBar();
            progressBar.setPrefHeight(23);
            setAlignment(Pos.CENTER);
        }

        @Override
        public void updateItem(T item, boolean empty) {
            super.updateItem(item, empty);
            if (item == null) {
                setGraphic(null);
                setText(null);
            } else {
                if (item.toString().equalsIgnoreCase("Processing")) {
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {

                            if (getGraphic() == null) {
                                setGraphic(progressBar);
                                progressBar.setProgress(-1);
                            } else {
                                ProgressBar objpProgressBar = (ProgressBar) getGraphic();
                                objpProgressBar.setProgress(-1);
                            }
                            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                        }
                    });
                } else {
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            if (getGraphic() == null) {
                                setGraphic(progressBar);
                                progressBar.setProgress(0);
                            } else {
                                ProgressBar objpProgressBar = (ProgressBar) getGraphic();
                                objpProgressBar.setProgress(0);
                            }
                            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                        }
                    });
                }
            }
        }
    } 
Run Code Online (Sandbox Code Playgroud)

显示工具提示的代码

public class ToolTip extends TableCell {

        @Override
        protected void updateItem(Object object, boolean selected) {
            if (object == null) {
                setGraphic(null);
                setText(null);
            }else{
                setText(object.toString());
                setTooltip(new Tooltip(object.toString()));
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

问题 -

如果我从TableProgressBarUpdator类中注释掉这两行,那么我可以看到第1,第3和第4列中每个单元格值的工具提示,但现在表格视图内容没有更新/刷新,当我联合评论这些行时,我是无法看到工具提示.

((TableColumn) table.getColumns().get(0)).setVisible(false);
((TableColumn) table.getColumns().get(0)).setVisible(true);
Run Code Online (Sandbox Code Playgroud)

总之,由于这两行我的工具提示渲染不起作用,如果我删除这两行,那么表视图内容不是刷新/更新.

Ron*_*nak 3

您不需要TableView手动更新。您的班级可能存在与此相关的问题TableView's column

您必须创建如下所示的类:

public static class Test{

private StringProperty name;

private Test() {
   name = new SimpleStringProperty();
}

public Test(String name) {
    this.name = new SimpleStringProperty(name);
}

public void setName(String name) {
    this.name.set(name);
}

public String getName() {
    return name.get();
}

public StringProperty nameProperty() {
    return name;
}
Run Code Online (Sandbox Code Playgroud)

}