我想用多色单元格创建 TableView。但我仍然需要在选定的行中突出显示单元格。我怎样才能做到这一点?例如,我有类 User - 具有 FXproperties 的 bean,并且我需要在“订单计数”列中绘制单元格,其中包含“1”。如果选择了包含此类单元格的行 - 也应选择此单元格。用户等级:
public class User {
private StringProperty firstName = new SimpleStringProperty();
private StringProperty lastName = new SimpleStringProperty();
private IntegerProperty ordersCount = new SimpleIntegerProperty(0);
public User(String firstName, String lastName, int ordersCount) {
this.firstName.set(firstName);
this.lastName.set(lastName);
this.ordersCount.set(ordersCount);
}
public final String getFirstName() {
return firstName.get();
}
public final String getLastName() {
return lastName.get();
}
public Integer getOrdersCount() {
return ordersCount.get();
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public void setOrdersCount(Integer ordersCount) {
this.ordersCount.set(ordersCount);
}
public StringProperty firstNameProperty(){
return firstName;
}
public StringProperty lastNameProperty(){
return lastName;
}
public IntegerProperty ordersCountProperty(){
return ordersCount;
}
}
css 文件:
.oneCell {
-fx-control-inner-background: lightsteelblue;
-fx-background-color: -fx-table-cell-border-color, -fx-control-inner-background;
-fx-background-insets: 0, 0 0 1 0;
}
主类:
public class JavaFXApplication13 extends Application {
@Override
public void start(Stage primaryStage) {
ObservableList<User> users = FXCollections.observableArrayList();
for(int i = 0; i < 100; i++){
users.add(Helper.createRandomUser());
}
TableView<User> tableView = new TableView<>();
tableView.setItems(users);
TableColumn<User, String> firstNameColumn = new TableColumn<>("First name");
firstNameColumn.setCellValueFactory(new PropertyValueFactory<User, String>("firstName"));
TableColumn<User, String> lastNameColumn = new TableColumn<>("Last name");
lastNameColumn.setCellValueFactory(new PropertyValueFactory<User, String>("lastName"));
TableColumn<User, Object> ordersCountColumn = new TableColumn<>("Orders count");
ordersCountColumn.setCellValueFactory(new PropertyValueFactory<User, Object>("ordersCount"));
ordersCountColumn.setCellFactory(new CellFactory());
tableView.getColumns().addAll(firstNameColumn, lastNameColumn, ordersCountColumn);
StackPane root = new StackPane();
root.getChildren().add(tableView);
Scene scene = new Scene(root, 500, 500);
scene.getStylesheets().add("javafxapplication13/main.css");
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private class CellFactory implements Callback<TableColumn<User, Object>, TableCell<User, Object>> {
@Override
public TableCell<User, Object> call(TableColumn<User, Object> p) {
TableCell<User, Object> cell = new TableCell<User, Object>(){
@Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
String text = getString();
setText(empty ? null : text);
setGraphic(null);
getStyleClass().remove("oneCell");
if (text != null && text.contains("1")) {
getStyleClass().add("oneCell");
}
}
private String getString() {
return getItem() == null ? "" : getItem().toString();
}
};
return cell;
}
}
}