我从 tableView 获取行。我使用 setRowFactory 来获取行,后来我使用它。
我需要重写 updateItem 方法来获取每一行,如果该行内的项目有错误,则使用红色,否则使用绿色。
行内部的项目是“历史”对象,因为行是历史的片段 - 历史,它是在应用程序中执行某些操作后创建的。
所以我有这样的东西:
@Override
protected void updateItem(History history, boolean empty) {
super.updateItem(history, empty);
if (empty) {
setStyle("");
} else if (history.isHasError() == true) {
getStyleClass().clear();
getStyleClass().add("errorHistoryRow");
} else if (history.isHasError() == false){
getStyleClass().clear();
getStyleClass().add("");
}
}
但是我需要将此 updateItem 添加到此方法中已定义的行中。
private void openErrorMessageAfterHoveringOverRow() {
historyTableView.setRowFactory(tableView -> {
final TableRow<History> row = new TableRow<>();
for (History history : model.getAllHistoryObservableArrayList()) {
***I NEED TO PUT IT HERE***
}
for (History his : model.getAllHistoryObservableArrayList()) {
row.hoverProperty().addListener((observable) -> {
History historyRow = row.getItem();
Point p = MouseInfo.getPointerInfo().getLocation();
int x = p.x;
int y = p.y;
Popup popup = new Popup();
popup.setX(x - 300);
popup.setY(y - 200);
TextArea ta = new TextArea();
AnchorPane layout = new AnchorPane();
Scene scene = new Scene(layout);
stageSingleton().setScene(scene);
if (row.isHover() && his.equals(historyRow)) {
ta.setText(row.getItem().getErrorMessage());
popup.getContent().addAll(ta);
stageSingleton().show();
popup.show(stageSingleton());
} else if (!row.isHover() && his.equals(historyRow)) {
popup.hide();
stageSingleton().close();
}
});
}
return row;
});
}
那么,我该如何实现方法 updateItem 到此方法中已定义的行呢?因为下面的方法有效,但我的项目中不能有 2 个不同的“setRowFactory”方法。所以我需要将它们合并到一种方法中。
public void test() {
historyTableView.setRowFactory(tableView -> new TableRow<History>() {
@Override
protected void updateItem(History history, boolean empty) {
super.updateItem(history, empty);
if (empty) {
setStyle("");
} else if (history.isHasError() == true) {
getStyleClass().clear();
getStyleClass().add("errorHistoryRow");
} else if (history.isHasError() == false){
getStyleClass().clear();
getStyleClass().add("");
}
}
});
}
最佳答案
不可能使用不同的rowFactory
s (至少你不能删除已经创建的行)。您需要组合 rowFactory
返回的行中的功能.
一些附加说明:
- 如果您的行变空,您还需要删除样式类。但是,清除样式类会干扰默认样式(您删除
table-row-cell
样式类)。在这种情况下使用伪类更容易。此外,添加空字符串作为样式类没有任何好处。 - 不要使用
InvalidationListener
对于hover
属性(property)。每次值变化时都会触发这种监听器,如果它从true
变化至false
或反之亦然。
public class HistoryRow extends TableRow<History> {
private static final PseudoClass ERROR = PseudoClass.getPseudoClass("error");
public HistoryRow() {
hoverProperty().addListener((o, oldValue, newValue) -> {
if (newValue) {
History historyRow = getItem();
if (historyRow != null && historyRow.isHasError()) {
// TODO: display popup here
}
}
});
}
@Override
protected void updateItem(History history, boolean empty) {
super.updateItem(history, empty);
pseudoClassStateChanged(ERROR, !empty && history != null && history.isHasError());
}
}
在 CSS 样式表中,从样式类更改为伪类需要调整选择器。您需要使用:error
伪类选择器而不是 .errorHistoryRow
类选择器。
可以通过添加 BooleanProperty
动态地将错误历史元素更改为非错误历史元素,反之亦然。至History
并在 updateItem
中添加/删除监听器方法,如有必要。
关于java - 从 TableView 和 UpdateItem 重写方法获取行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50466616/