JavaFX.TableView 中的日期格式

Format of Date in the JavaFX.TableView

我 运行 遇到了问题。

我有一个 POJO class:

public class PatientEntity {

    private int id;
    private Date dateDoc;
    private String secondname;
    private String firstname;
    private String thirdname;

    @Id
    @Column(name = "ID")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    @Column(name = "Date_doc")
    public Date getDateDoc() {
        return dateDoc;
    }

    public void setDateDoc(Date dateDoc) {
        this.dateDoc = dateDoc;
    }

我正在使用JavaFX.TableView来填充:

columnID.setCellValueFactory(new PropertyValueFactory<PatientEntity, Integer>("id"));
columnDate.setCellValueFactory(new PropertyValueFactory<PatientEntity, Date>("dateDoc"));
columnSecondname.setCellValueFactory(new PropertyValueFactory<PatientEntity, String>("secondname"));
columnFirstname.setCellValueFactory(new PropertyValueFactory<PatientEntity, String>("firstname"));
columnThirdname.setCellValueFactory(new PropertyValueFactory<PatientEntity, String>("thirdname"));

tableView.setItems(FXCollections.observableList(patients));

结果: the result of running

它工作正常,但我需要 Date 的另一种格式,例如 TableView 中的 dd.mm.yyyy。那我该怎么做呢?如您所见,TableView 包含对象,而不是简单的字符串。

已编辑 #1

private synchronized void updateTableView(List <PatientEntity> patients){

    columnID.setCellValueFactory(new PropertyValueFactory<PatientEntity, Integer>("id"));
    columnDate.setCellFactory(column -> {
        TableCell<PatientEntity, Date> cell = new TableCell<PatientEntity, Date>() {
            private SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");

            @Override
            protected void updateItem(Date item, boolean empty) {
                super.updateItem(item, empty);
                if(empty) {
                    setText(null);
                }
                else {
                    this.setText(format.format(item));

                }
            }
        };

        return cell;
    });

    columnSecondname.setCellValueFactory(new PropertyValueFactory<PatientEntity, String>("secondname"));
    columnFirstname.setCellValueFactory(new PropertyValueFactory<PatientEntity, String>("firstname"));
    columnThirdname.setCellValueFactory(new PropertyValueFactory<PatientEntity, String>("thirdname"));
    tableView.setItems(FXCollections.observableList(patients));
}

Such result

已编辑 #2

private void updateTableView(){
    Thread threadConnectingToDB = new Thread(() -> {
        try {
            List <PatientEntity> patients;
            PatientDAO patientDAO = FactoryDAO.getInstance().getPatientDAO();
            if(patientDAO.getAllPatients() instanceof List){
                patients = (List) patientDAO.getAllPatients();
            } else {
                patients = new ArrayList(patientDAO.getAllPatients());
            }
            columnID.setCellValueFactory(new PropertyValueFactory<PatientEntity, Integer>("id"));
            columnDate.setCellValueFactory(new PropertyValueFactory<PatientEntity, Date>("doc_date"));
            columnDate.setCellFactory(column -> {
                TableCell<PatientEntity, Date> cell = new TableCell<PatientEntity, Date>() {
                    private SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");

                    @Override
                    protected void updateItem(Date item, boolean empty) {
                        super.updateItem(item, empty);
                        if(empty) {
                            setText(null);
                        }
                        else {
                            if(item != null)
                            this.setText(format.format(item));
                        }
                    }
                };

                return cell;
            });

            columnSecondname.setCellValueFactory(new PropertyValueFactory<PatientEntity, String>("secondname"));
            columnFirstname.setCellValueFactory(new PropertyValueFactory<PatientEntity, String>("firstname"));
            columnThirdname.setCellValueFactory(new PropertyValueFactory<PatientEntity, String>("thirdname"));
            tableView.setItems(FXCollections.observableList(patients));


        } catch (SQLException e) {
            try {
                ErrorStage.display("Произошла ошибка при подключении к базе данных");
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    });
    threadConnectingToDB.start();
    threadConnectingToDB.interrupt();
}

解决方案

感谢@mr mcwolf 有一个解决方案!

我们需要两种方法: 第一个方法是一个配置方法,我们在初始化时调用一次:

private void configureTableView(){
    columnID.setCellValueFactory(new PropertyValueFactory<PatientEntity, Integer>("id"));
    columnDate.setCellFactory(column -> {
        TableCell<PatientEntity, Date> cell = new TableCell<PatientEntity, Date>() {
            private SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");

            @Override
            protected void updateItem(Date item, boolean empty) {
                super.updateItem(item, empty);
                if(empty) {
                    setText(null);
                }
                else {
                    this.setText(format.format(item));

                }
            }
        };

        return cell;
    });
    columnDate.setCellValueFactory(new PropertyValueFactory<PatientEntity, Date>("dateDoc"));
    columnSecondname.setCellValueFactory(new PropertyValueFactory<PatientEntity, String>("secondname"));
    columnFirstname.setCellValueFactory(new PropertyValueFactory<PatientEntity, String>("firstname"));
    columnThirdname.setCellValueFactory(new PropertyValueFactory<PatientEntity, String>("thirdname"));

}

并且我们需要一个方法来填充 tableView:

private synchronized void fillTableView(List <PatientEntity> patientsList){

    Thread threadConnectingToDB = new Thread(() -> {
        try {
            List<PatientEntity> patients = patientsList;
            if(patients == null) {
                PatientDAO patientDAO = FactoryDAO.getInstance().getPatientDAO();
                if (patientDAO.getAllPatients() instanceof List) {
                    patients = (List) patientDAO.getAllPatients();
                } else {
                    patients = new ArrayList(patientDAO.getAllPatients());
                }
            }
            tableView.setItems(FXCollections.observableList(patients));
        } catch (SQLException e) {
            try {
                ErrorStage.display("Error Connection");
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    });
    threadConnectingToDB.start();
    threadConnectingToDB.interrupt();
}

试试这个

columnDate.setCellFactory(column -> {
    TableCell<PatientEntity, Date> cell = new TableCell<PatientEntity, Date>() {
        private SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");

        @Override
        protected void updateItem(Date item, boolean empty) {
            super.updateItem(item, empty);
            if(empty) {
                setText(null);
            }
            else {
                setText(format.format(item));
            }
        }
    };

    return cell;
});