简体   繁体   中英

JavaFX cells colors in TableView

I want create TableView with multicolor cells. But i still need highlight cells in selected rows. How can i do that? For example i have class User - bean with FXproperties, and i need paint cells in column "Orders count", that contains "1". If row, containing such cell, is selected - this cell should be selected too. User class:

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 file:

.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;
}

Main class:

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;
    }
 }

}

Brute force solution:

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM