JavaFx Tableview,如何在同一个工厂中添加监听器和覆盖updateItem
JavaFx Tableview, how to add listener and overwrite updateItem in the same factory
我目前正在从事一个带有 TableView 元素的小型学习项目。我正在根据其内容更改每行的背景颜色:
TableView.setRowFactory((row) -> new TableRow<Person>() {
@Override
public void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
switch (person.getPersonStatus()) {
case ST:
setStyle("-fx-control-inner-background: " + StatusColor.B_LIGHT.getMyColorValue() + "; -fx-text-fill: #fff;");
break;
case CD:
setStyle("-fx-control-inner-background: " + StatusColor.D_LIGHT.getMyColorValue() + "; -fx-text-fill: #fff;");
break;
}
}
我还想在双击该行时获取该行中对象的引用。我为此使用此代码:
TableView.setRowFactory((row) -> {
TableRow<Person> row = new TableRow<>();
row.setOnMouseClicked(event -> {
if (event.getClickCount() == 2 && (!row.isEmpty())) {
Person rowData = row.getItem();
System.out.println(rowData);
}
});
return row;
});
但这不能一起工作(我假设是因为我正在分配两个相互覆盖的工厂)。有人可以帮我将两个代码示例组合成一个有效的代码示例吗?如何在工厂中覆盖函数(updateItem)并同时附加监听器?
此致
只需将侦听器添加到您在第一个代码块中创建的TableRow
:
TableView.setRowFactory((tv) -> {
TableRow<Row> row = new TableRow<Person>() {
@Override
public void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
switch (person.getPersonStatus()) {
case ST:
setStyle("-fx-control-inner-background: " + StatusColor.B_LIGHT.getMyColorValue() + "; -fx-text-fill: #fff;");
break;
case CD:
setStyle("-fx-control-inner-background: " + StatusColor.D_LIGHT.getMyColorValue() + "; -fx-text-fill: #fff;");
break;
}
}
};
row.setOnMouseClicked(event -> {
if (event.getClickCount() == 2 && (!row.isEmpty())) {
Person rowData = row.getItem();
System.out.println(rowData);
}
});
return row;
});
我目前正在从事一个带有 TableView 元素的小型学习项目。我正在根据其内容更改每行的背景颜色:
TableView.setRowFactory((row) -> new TableRow<Person>() {
@Override
public void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
switch (person.getPersonStatus()) {
case ST:
setStyle("-fx-control-inner-background: " + StatusColor.B_LIGHT.getMyColorValue() + "; -fx-text-fill: #fff;");
break;
case CD:
setStyle("-fx-control-inner-background: " + StatusColor.D_LIGHT.getMyColorValue() + "; -fx-text-fill: #fff;");
break;
}
}
我还想在双击该行时获取该行中对象的引用。我为此使用此代码:
TableView.setRowFactory((row) -> {
TableRow<Person> row = new TableRow<>();
row.setOnMouseClicked(event -> {
if (event.getClickCount() == 2 && (!row.isEmpty())) {
Person rowData = row.getItem();
System.out.println(rowData);
}
});
return row;
});
但这不能一起工作(我假设是因为我正在分配两个相互覆盖的工厂)。有人可以帮我将两个代码示例组合成一个有效的代码示例吗?如何在工厂中覆盖函数(updateItem)并同时附加监听器?
此致
只需将侦听器添加到您在第一个代码块中创建的TableRow
:
TableView.setRowFactory((tv) -> {
TableRow<Row> row = new TableRow<Person>() {
@Override
public void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
switch (person.getPersonStatus()) {
case ST:
setStyle("-fx-control-inner-background: " + StatusColor.B_LIGHT.getMyColorValue() + "; -fx-text-fill: #fff;");
break;
case CD:
setStyle("-fx-control-inner-background: " + StatusColor.D_LIGHT.getMyColorValue() + "; -fx-text-fill: #fff;");
break;
}
}
};
row.setOnMouseClicked(event -> {
if (event.getClickCount() == 2 && (!row.isEmpty())) {
Person rowData = row.getItem();
System.out.println(rowData);
}
});
return row;
});