gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:23:07 25 4
gpt4 key购买 nike

我的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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com