gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 20:13:46 24 4
gpt4 key购买 nike

在我的 JavaFX TableView 中,我有一个 TableColumn,我已将 Cell Factory 设置为呈现 ProgressBar 和对于其他 TableColumn,我已将 Cell Factory 设置为显示 ToolTip。就像下图一样。第二列显示进度条,其他 3 列渲染以显示工具提示,其中包含要显示的简单字符串值。

enter image description here

我遇到的问题是 TableView 没有在表中显示/显示更新的值,即 UI 没有验证/刷新/绘制 TableView 元素。如果我单击 ColumnHeader 对任何列进行排序,那么只有我可以看到 TableView 更新。手动对表格列进行排序以刷新表格内容没有意义,因此我搜索并找到了显示/隐藏表格列以更新表格 View 的解决方案。

为了解决这个问题,我在下面编写了一个代码来解决 TableView 更新/刷新问题,但是由于这段代码,现在 ToolTip 不可见。

在每个特定时间间隔后更新 TableView 的代码

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


}
}

开始更新表格 View

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

停止更新表格 View

public void stopUpdatingTableProgress() {
keepUpdating = false;
}

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

显示进度条 TableView 的代码。

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 列中看到每个单元格值的工具提示,但现在表格 View 内容没有更新/刷新,当我 UN - 注释这些行我看不到工具提示。

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

总而言之,由于这两行,我的工具提示渲染器无法正常工作,如果我删除这两行,则 TableView 内容不会刷新/更新。

最佳答案

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

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

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

关于java - 无法在 JavaFX TableView 单元格值上显示工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13700580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com