在JAVAFX表格视图中设置值-对象与另一个对象

在JAVAFX表格视图中设置值-对象与另一个对象,java,object,javafx,properties,tableview,Java,Object,Javafx,Properties,Tableview,MyDTO对象包含另外两个对象。我需要将这些值也设置到表中 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

MyDTO对象包含另外两个对象。我需要将这些值也设置到表中

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

}
PatientTo被这些属性完全封装

    private int patienID;
    private String patientName;
    private int patienAge;
    private String ContactNumber;
private int doctorID;
private String doctorName;
private String hospital;
DoctorDTO也完全封装了这些属性

    private int patienID;
    private String patientName;
    private int patienAge;
    private String ContactNumber;
private int doctorID;
private String doctorName;
private String hospital;
签出链接

它适用于
ListCell
,但我认为您可以轻松地将其应用于
TableView


也许project中的代码也会有所帮助。

您可以这样做,例如

@FXML
private TableColumn<AppointmentDTO, String> patientNameColumn ;

// ...

public void initialize() {

    // ...

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

    // ...
}
@FXML
private TableColumn patientNameColumn;
// ...
公共无效初始化(){
// ...
patientNameColumn.setCellValueFactory(cellData->
新的SimpleStringProperty(cellData.getValue().GetPatientTo().getPatientName());
// ...
}

您真的应该为此使用单元格值工厂,而不是单元格工厂。这是您需要指定的值(即数据),我认为如果您将
fx:id
s放在表列上并直接引用它们,您的代码会更干净。我厌倦了。但是这个错误来了。不兼容类型:lambda表达式SimpleStringProperty中的错误返回类型无法转换为ObservalEvalue,其中CAP#1是一个新类型变量:CAP#1从捕获?Wow.扩展对象。。成功了。这意味着everu列必须用变量名初始化。感谢you@kasun无论如何,这可能是个好主意。您不希望因为决定对FXML文件中的列进行重新排序,或者因为您添加了一个新的列而不是在末尾,所以所有内容都中断。太好了!非常感谢你。