java - 在 JAVAFX TableView 中设置值 - 对象与另一个对象

标签 java object javafx properties tableview

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

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;

最佳答案

例如,您可以这样做

@FXML
private TableColumn<AppointmentDTO, String> patientNameColumn ;

// ...

public void initialize() {

    // ...

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

    // ...
}

关于java - 在 JAVAFX TableView 中设置值 - 对象与另一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46309376/

相关文章:

javascript - XMLHttpRequest 对象

JavaFX - 为什么将节点多次添加到一个 Pane 或添加到不同的 Pane 会导致错误?

java - Apache Derby # 符号上出现词法错误?

java - 对 Map.containsValue 方法的困惑

javascript - 什么时候用对象,什么时候用数组

java - 将 ObservableList 中的修改合并到原始集合

java - 在从 TextField (FXML) 扩展的自定义类中分配 boolean 值时出错

java - 以字符串格式标记代数表达式

java - 设备上的 Webview 应用程序分辨率太大

javascript - 对象/类内的函数声明