我的DTO對象包含另外兩個對象。我需要將這些值也設置在表格中。在JAVAFX表格視圖中設置值 - 與另一個對象的對象
controller.java
package controller;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXDatePicker;
import com.jfoenix.controls.JFXTextField;
import dto.AppointmentDTO;
import dto.DoctorDTO;
import dto.PatientDTO;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
public class AddAppointmentController implements Initializable {
@FXML
private TableView<AppointmentDTO> tblView;
private ObservableList<AppointmentDTO> tblData;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
tblData = FXCollections.observableArrayList();
tblView.getColumns().get(0).setCellValueFactory(new PropertyValueFactory<>("appointmentID"));
tblView.getColumns().get(1).setCellValueFactory(new PropertyValueFactory<>("appointDate"));
tblView.getColumns().get(2).getColumns().get(0).setCellValueFactory(new PropertyValueFactory<>("patientName"));
tblView.getColumns().get(2).getColumns().get(1).setCellValueFactory(new PropertyValueFactory<>("patienAge"));
tblView.getColumns().get(2).getColumns().get(2).setCellValueFactory(new PropertyValueFactory<>("ContactNumber"));
tblView.getColumns().get(3).getColumns().get(0).setCellValueFactory(new PropertyValueFactory<>("doctorName"));
tblView.getColumns().get(3).getColumns().get(1).setCellValueFactory(new PropertyValueFactory<>("hospital"));
tblData.add(new AppointmentDTO(12, "201", new PatientDTO(2, "uh", 12, "hj"), new DoctorDTO(4, "ghj", "ghj")));
tblView.setItems(tblData);
}
}
AppointmentDTO.java ,這是完全封裝類。
package dto;
public class AppointmentDTO {
private int appointmentID;
private String appointDate;
private PatientDTO patientDTO;
private DoctorDTO doctorDTO;
}
PatientDTO是具有這些性質的完全封裝;
private int patienID;
private String patientName;
private int patienAge;
private String ContactNumber;
DoctorDTO也完全具有這些特性的封裝;
private int doctorID;
private String doctorName;
private String hospital;
順便說一句,我認爲,如果你把'FX您的代碼將是更清潔的:上表中的列id's和直接引用他們。 –