Javafx通过数据库值更改特殊的行颜色

时间:2019-08-03 04:34:11

标签: javafx tableview

我在表格视图中选择了一行,然后单击按钮panggilAntrian。当我单击该按钮时,boolean_antri为true,我希望将行的颜色更改为绿色。

@FXML
private TableView<Antrian> antrianTable;
@FXML
private TableColumn<Antrian, Integer> id;
@FXML
private TableColumn<Antrian, String> tanggal;
@FXML
private TableColumn<Antrian, Integer> no_antrian;
@FXML
private TableColumn<Antrian, String> jam_ambil;
@FXML
private TableColumn<Antrian, String> jam_panggil;
@FXML
private TableColumn<Antrian, Boolean> boolean_antrian;

@FXML
    private void actionPanggilAntrian(ActionEvent event) throws IOException {
        DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        Date time = new Date();
        String waktuPanggil = timeFormat.format(time);

        int id = antrian.getId();
        boolean boolean_antri = false;

        String sql = "UPDATE antrian SET jam_panggil = '" + waktuPanggil + "', boolean_antrian = true where id = '" + id + "'";

        try (Connection conn = connection.connection();
             PreparedStatement pstmt = conn.prepareStatement(sql)
            ){  
                pstmt.executeUpdate();
                boolean_antri = true;
                antrian.setId(id);
                antrian.setBooleanAntrian(boolean_antri);
                txtNoDipanggil.setText(Integer.toString(idAntrian));

                    boolean_antrian.setCellFactory(col -> { //There's an error: java.lang.NullPointerException in boolean_antrian
                        TableCell<Antrian, Boolean> cell = new TableCell<Antrian, Boolean>() {
                            @Override
                            public void updateItem(Boolean item, boolean empty) {
                                super.updateItem(item, empty);
                                if (item) {
                                    TableRow currentRow = new TableRow();
                                    currentRow.setStyle("-fx-background-color: green");
                                }
                            }
                        };
                        return cell;
                    });
            } catch(SQLException e){
                System.out.println(e);
            }
    }

出现错误: boolean_antrian.setCellFactory中的java.lang.NullPointerException。 boolean_antrian的值为null。 我的代码有什么问题?

0 个答案:

没有答案