当自定义 TreeTableView 内的单元格然后选择任意行时,条形图的颜色采用默认的 modena 样式,到目前为止它是正确的。
失去焦点后选择栏的文本不再是默认的黑色
如何在 JavaFX 中解决这个问题,有没有优雅的解决方案?
我试过
tableRowProperty().flatMap(TreeTableRow::focusedProperty).addListener((obs, wasSelected, isNowSelected) -> updateTextFill());
但失去焦点时它也无法正常工作
符合预期
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableCell;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableRow;
import javafx.scene.control.TreeTableView;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Callback;
public class SelectionBarColorFocus extends Application {
BorderPane bp = new BorderPane();
@Override
public void start(Stage primaryStage) {
TreeTableView<Person> treeTableView = new TreeTableView<>();
// Criar colunas
TreeTableColumn<Person, String> nameColumn = new TreeTableColumn<>("Name");
nameColumn.setCellValueFactory(cellData -> cellData.getValue().getValue().nameProperty());
nameColumn.setCellFactory((TreeTableColumn<Person, String> param) -> {
TreeTableCell<Person, String> cell = new TreeTableCell<Person, String>() {
final ImageView imv = new ImageView();
final Text text = new Text("");
HBox hbox = new HBox(imv, text);
{
tableRowProperty().flatMap(TreeTableRow::selectedProperty).addListener((obs, wasSelected, isNowSelected) -> updateTextFill());
// tableRowProperty().flatMap(TreeTableRow::focusedProperty).addListener((obs, wasSelected, isNowSelected) -> updateTextFill());
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
text.setFill(Color.ORANGE);
text.setText(item);
setGraphic(hbox);
}else {
text.setText("");
setGraphic(null);
}
}
private void updateTextFill() {
text.setFill(getTableRow().isSelected() ? Color.WHITE: Color.ORANGE);
}
};
return cell;
}
);
TreeTableColumn<Person, String> addressColumn = new TreeTableColumn<>("Address");
addressColumn.setCellValueFactory(cellData -> cellData.getValue().getValue().addressProperty());
TreeTableColumn<Person, Number> ageColumn = new TreeTableColumn<>("Age");
ageColumn.setCellValueFactory(cellData -> cellData.getValue().getValue().ageProperty());
ageColumn.setCellFactory((TreeTableColumn<Person, Number> param) -> {
TreeTableCell<Person, Number> cell = new TreeTableCell<Person, Number>() {
boolean flat = false;
final Text text = new Text("");
{
focusedProperty().addListener((obs, wasFocused, isNowFocused) -> {
System.out.println("jorge focused="+isNowFocused);
}
);
}
@Override
public void updateItem(Number item, boolean empty) {
super.updateItem(item, empty);
if(flat == false) {
tableRowProperty().flatMap(TreeTableRow::selectedProperty).addListener((obs, wasSelected, isNowSelected) -> updateTextFill(item, empty));
flat = true;
}
updateTextFill(item, empty);
}
private void updateTextFill(Number item, boolean empty) {
if(item != null && item.intValue() != -1) {
text.setFill(Color.BLUE);
text.setText(item.toString());
setGraphic(text);
}else {
text.setText("");
setText("");
setGraphic(null);
}
text.setFill(getTableRow().isSelected() ? Color.WHITE: Color.BLUE);
}
};
return cell;
});
nameColumn.setPrefWidth(150);
addressColumn.setPrefWidth(200);
ageColumn.setPrefWidth(100);
treeTableView.getColumns().addAll(nameColumn, addressColumn, ageColumn);
TreeItem<Person> rootItem = new TreeItem<>(new Person("Persons", "", -1));
rootItem.getChildren().addAll(
new TreeItem<>(new Person("Alice", "456 Park Ave", 25)),
new TreeItem<>(new Person("Bob", "789 Broadway", 35)),
new TreeItem<>(new Person("Carol", "987 Elm St", 40)),
new TreeItem<>(new Person("John", "123 Main St", 40))
);
rootItem.setExpanded(true);
treeTableView.setRoot(rootItem);
Button b = new Button("Row Lost Focus");
StackPane st = new StackPane();
st.getChildren().add(b);
bp.setCenter(treeTableView);
bp.setBottom(st);
Scene scene = new Scene(bp, 500, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX TreeTableView SelectionBar Lost Focus");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public static class Person {
private StringProperty name;
private StringProperty address;
private IntegerProperty age;
public Person(String name, String address, int age) {
this.name = new SimpleStringProperty(name);
this.address = new SimpleStringProperty(address);
this.age = new SimpleIntegerProperty(age);
}
public StringProperty nameProperty() {
return name;
}
public StringProperty addressProperty() {
return address;
}
public IntegerProperty ageProperty() {
return age;
}
}
}