2017-09-19 78 views
1

我的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; 
+0

顺便说一句,我认为,如果你把'FX您的代码将是更清洁的:上表中的列id's和直接引用他们。 –

回答

0

你可以做,例如,

@FXML 
private TableColumn<AppointmentDTO, String> patientNameColumn ; 

// ... 

public void initialize() { 

    // ... 

    patientNameColumn.setCellValueFactory(cellData -> 
     new SimpleStringProperty(cellData.getValue().getPatientDTO().getPatientName())); 

    // ... 
} 
+0

我累了。但是这个错误来了。我不知道它为什么以及如何解决它。 – kasun

+0

不兼容的类型:lambda表达式中的返回类型不正确 SimpleStringProperty无法转换为ObservableValue 其中CAP#1是新鲜的类型变量: CAP#1从捕获? – kasun

+0

@ kasun查看更新 –

0

检查this环节出

这对ListCell,但我认为你可以很容易地将其应用到TableView

也许this项目的代码也可能有帮助。

+0

你应该真的使用cell * value *工厂,而不是cell factory。这是你需要指定的值(即数据) –

相关问题