2

My DTO Object contain another two objects. I need to set the value of those also in to the table.

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 And this is fully encapsulated class.

package dto;
  public class AppointmentDTO {   
        private int appointmentID;
        private String appointDate;
        private PatientDTO patientDTO;
        private DoctorDTO doctorDTO;
    }

PatientDTO is fully encapsulated with these properties;

    private int patienID;
    private String patientName;
    private int patienAge;
    private String ContactNumber;

DoctorDTO is also fully encapsulated with these properties;

private int doctorID;
private String doctorName;
private String hospital;
1
  • As an aside, I think your code would be much cleaner if you put fx:ids on the table columns and referenced them directly. Commented Sep 19, 2017 at 21:55

2 Answers 2

3

You can do, for example,

@FXML
private TableColumn<AppointmentDTO, String> patientNameColumn ;

// ...

public void initialize() {

    // ...

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

    // ...
}
Sign up to request clarification or add additional context in comments.

5 Comments

i tired it. But this error comes. I don't know why is it and how to fix it.
incompatible types: bad return type in lambda expression SimpleStringProperty cannot be converted to ObservableValue<CAP#1> where CAP#1 is a fresh type-variable: CAP#1 extends Object from capture of ?
Wow.. that worked. That means everu column must be initialize with variable names. Thank you
@kasun That's probably a good idea anyway. You wouldn't want everything to break because you decided to reorder the columns in the FXML file, or because you added a new column not at the end, etc.
Great! Thank you very much.
0

Check this link out

It's for ListCell, but I think you can easily apply it to TableView.

Maybe the code from this project may help as well.

1 Comment

You should really use a cell value factory for this, not a cell factory. It's the value (i.e. data) you need to specify

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.