1
我想用多色單元格創建TableView。但我仍然需要選中行中的高亮單元格。我怎樣才能做到這一點?例如,我有FXproperties類的用戶bean,我需要在「Orders count」列中包含「1」的繪製單元格。如果選擇包含這樣的單元格的行,則應該選擇該單元格。 用戶類:TableView中的JavaFX單元格顏色
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;
}
}
}
我認爲你還需要爲「oneCell」定義選擇,填充,聚焦等CSS僞類。請參閱JavaFX CSS指南。此外,如果僅查找字符串「1」,請使用equals()而不是contains()。 –
爲了更好,更快的幫助,發佈[SSCCE](http://sscce.org/) –