无法在JavaFX TableView单元格值上显示工具提示 [英] Unable to show the Tool Tip on the JavaFX TableView cell value

查看:291
本文介绍了无法在JavaFX TableView单元格值上显示工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 JavaFX TableView 我有一个 TableColumn 我已将Cell Factory设置为呈现 ProgressBar 以及其他 TableColumn s我将Cell Factory设置为显示工具提示。如下图所示。第二列显示进度条,其他3列呈现以显示工具提示,其中包含要显示的简单字符串值。

In my JavaFX TableView I have one TableColumn on which I have set Cell Factory to render ProgressBar and for other TableColumns I have set Cell Factory to show ToolTip. Like the image below. Second Column is showing Progress Bar and other 3 Columns are render to show Tool tip, that has simple string values to show.

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

I was getting issue in which the TableView was not displaying/showing updated values in the table i.e UI is not validating/refreshing/painting the TableView elements. If I clicked on ColumnHeader to sort any column then only I can see the TableView updating. Manually sort the table column to refresh the table content is not making sense so I have searched and found solution to show/hide the Table Columns for updating the Table View.

为了解决这个问题,我在下面编写了一个代码来解决 TableView 更新/刷新问题,但由于这个原因现在代码 ToolTip 未显示。

To resolved the issue I have written a code below to solve the TableView Updating/Refreshing issue but due to this code now ToolTip are not getting visible.

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

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


        }
    }

开始更新表格视图

public void startUpdatingTableProgress() {
    keepUpdating = true;
    TableProgressBarUpdator tpu = new TableProgressBarUpdator(table);
    tpu.start();
}

停止更新表格视图

public void stopUpdatingTableProgress() {
        keepUpdating = false;
    }

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

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

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

显示工具提示的代码

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

问题 -

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

((TableColumn) table.getColumns().get(0)).setVisible(false);
((TableColumn) table.getColumns().get(0)).setVisible(true);

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

In all due to these two lines my Tool Tip Render is not working and If I remove these two lines then Table View Content are not Refreshing/Updating.

推荐答案

您无需手动更新 TableView 。您的类中可能存在与 TableView的列关联的问题。

You don't need to update TableView manually. may be there are problem in your class associated with that TableView's column.

您必须按以下方式创建课程:

You have to create class as given below :

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

}

这篇关于无法在JavaFX TableView单元格值上显示工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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