문제가 발생했습니다. 채우기 위해JavaFX.TableView의 날짜 형식
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));
:
은 내가 POJO 클래스가 the result of running
그것은 잘 작동하지만 TableView
에 dd.mm.yyyy와 같은 Date
의 다른 형식이 필요합니다. 그러면 내가 어떻게 할 수 있니? 보시다시피, 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));
}
편집 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();
}
당신이 포맷터를 사용 했 시도? 첫 번째 결과 : FormatFormatter = new SimpleDateFormat ("dd/MM/yy");'적용 :'formatter.format (dateDoc);' –
SimpleDateFormat의 작동 방식을 알고 JavaFX.TableView에서 사용하는 방법을 모른다. . 나는 오버 라이딩이 필요하다고 생각하지만 그것을하는 법을 모른다. – SkaaRJ
아마 다른 곳에서 테이블을 구성 할 것이다. 편집 된 코드에는 'columnDate.setCellValueFactory'가 없지만 열에는 데이터가 있습니다. –