Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format of Date in the JavaFX.TableView

I have ran into a problem.

I have a 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;
    }

I'm using JavaFX.TableView in such way to fill:

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

In result: the result of running

It works fine, but I need another format of Date such as dd.mm.yyyy in the TableView. So how can I do that? As you can see, TableView contains objects, not simple strings.

Edited #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

Edited #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();
}

Solution

Thank to @mr mcwolf there is a solution!

We need two methods: The first method is a configuration method, which we call once during the initialization:

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"));

}

And we need a method to fill the 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();
}
like image 899
SkaaRJ Avatar asked Nov 25 '17 09:11

SkaaRJ


1 Answers

try this

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;
});
like image 76
mr mcwolf Avatar answered Nov 13 '22 06:11

mr mcwolf