Ska*_*aRJ 5 java javafx date date-format
我遇到了一个问题。
我有一个 POJO 类:
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;
}
Run Code Online (Sandbox Code Playgroud)
我正在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));
Run Code Online (Sandbox Code Playgroud)
结果: 运行的结果
它工作正常,但我需要另一种格式,Date例如 dd.mm.yyyy 在TableView. 那我该怎么做呢?如您所见,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));
}
Run Code Online (Sandbox Code Playgroud)
编辑 #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();
}
Run Code Online (Sandbox Code Playgroud)
解决方案
感谢@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"));
}
Run Code Online (Sandbox Code Playgroud)
我们需要一个方法来填充 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();
}
Run Code Online (Sandbox Code Playgroud)
mr *_*olf 12
尝试这个
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;
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11596 次 |
| 最近记录: |