Изменить спецификацию c цвет строки в javaFX - PullRequest
1 голос
/ 30 апреля 2020

Вот код, который я пишу, чтобы изменить цвет строки, где метр кожи <200, но я сталкиваюсь с исключением нулевого указателя в условии if. Сначала я получаю все данные из базы данных и добавляю их все в табличное представление, чтобы не ожидать исключения нулевого указателя. В чем проблема? </p>

    @FXML
    TableView<Leather> tableView;
    ObservableList<Leather> data = FXCollections.observableArrayList();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        tableView.setEditable(true);
        codeCol.setCellValueFactory(new PropertyValueFactory<>("code"));
        colorCol.setCellValueFactory(new PropertyValueFactory<>("color"));
        meterCol.setCellValueFactory(new PropertyValueFactory<>("meter"));

        indexCol.setCellFactory(col -> new TableCell<Task, String>() {
            @Override
            public void updateIndex(int index) {
                super.updateIndex(index);
                if (isEmpty() || index < 0) {
                    setText(null);
                } else {
                    setText(Integer.toString(index+1));
                }
            }
        });

        data.addAll(storeService.getAll());
        tableView.setItems(data);

        tableView.setRowFactory(tv -> new TableRow<Leather>(){
            @Override
            protected void updateItem(Leather item, boolean empty) {
                super.updateItem(item,empty);
                if (item.getMeter()<200){
                    setStyle("-fx-background-color: #DB8A6B");
                }
            }
        });
     }

1 Ответ

3 голосов
/ 30 апреля 2020

Вам нужно обработать все дела в вашем rowFactory (так же, как вы делаете в своем cellFactory):

    tableView.setRowFactory(tv -> new TableRow<Leather>(){
        @Override
        protected void updateItem(Leather item, boolean empty) {
            super.updateItem(item,empty);
            if (empty || item == null) {
                setStyle("");
            } else if (item.getMeter()<200){
                setStyle("-fx-background-color: #DB8A6B");
            } else {
                setStyle("");
            }
        }
    });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...