TableView中的JavaFX单元格颜色

时间:2013-01-07 12:12:41

标签: java css javafx-2

我想用多色单元格创建TableView。但我仍然需要在选定的行中突出显示单元格。我怎样才能做到这一点?例如,我有类用户 - 具有FXproperties的bean,我需要在“Orders count”列中绘制包含“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;
    }
 }

}

1 个答案:

答案 0 :(得分:1)

蛮力解决方案:

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);
                updateStyles(false);

                getTableView().getSelectionModel().getSelectedItems().addListener(new ListChangeListener<User>() {

                    @Override
                    public void onChanged(Change<? extends User> change) {
                        while (change.next()) {
                            if (getTableRow() != null) {
                                if (change.wasRemoved() && change.getRemoved().contains(getTableRow().getItem())) {
                                    updateStyles(false);
                                }
                                if (change.wasAdded() && change.getList().contains(getTableRow().getItem())) {
                                    updateStyles(true);
                                }
                            }
                        }
                    }
                });
            }

            private String getString() {
                return getItem() == null ? "" : getItem().toString();
            }

            private void updateStyles(boolean becameSelected) {
                if (becameSelected) {
                    getStyleClass().remove("oneCell");
                } else {
                    if (getString() != null && getString().contains("1")) {
                        getStyleClass().add("oneCell");
                    }
                }
            }
        };
        return cell;
    }
}