Home > Software engineering >  How to retrieve Datepicker Value from a selected cell in TableView
How to retrieve Datepicker Value from a selected cell in TableView

Time:11-02

Basically I have tableview and a FORM. I enter value in the form and they are displayed inside the table view via a database. It looks like this: FORM and TableView

When I click on the row to select it , I want to retrieve the DatePicker value from the column Begin Datum back to the Datepicker field.

I have a onm ouseClicked method like this to retrieve the text/vlaues from the selected row back to the FORM

@FXML
void getSelected(MouseEvent event) {

index = tableViewBooking.getSelectionModel().getSelectedIndex();

if (index <= -1) {
    
    return;
}

beginTime.setText(tableColSTime.getCellData(index).toString());
endTime.setText(tableColETime.getCellData(index).toString());
beginDate.setValue(tableColDate.getCellData(index).toString()); // problem has somewith with `toString()?`
reminderDesc.setText(tableColName.getCellData(index).toString());


}

Relevant primaryController code:

public class PrimaryController {
    
    ObservableList<String>  intervalList = 

FXCollections.observableArrayList("Täglich","Wochelich","Monatlich");
        ObservableList<String>  projectTypeList = FXCollections.observableArrayList("ProjeKt Iot","Projekt Data & Analytics","Projekt SAP Intelligent Enterprise",
                "Projekt Prozess & Betrieb"," Projekt Moderne Software Architekturen ");
        
   

    @FXML
    private DatePicker beginDate;

    @FXML
    private TextField beginTime;

    @FXML
    private Button clearButton;
    
    @FXML
    private DatePicker endDate;

    @FXML
    private TextField endTime;
    
    @FXML
    private TextField reminderDesc;

    @FXML
    private Button saveButton;
    
    @FXML
    private ComboBox cycleComboBox;
    
    @FXML
    private ComboBox projectComboBox;
    
    @FXML
    private JFXListView<String> listofdata;
    
    @FXML
    private Button modifyButton;
    
    @FXML
    private ResourceBundle resources;

    @FXML
    private URL url;
    
    // Table View
    @FXML
    public TableView<Booking> tableViewBooking;
    
    @FXML
    public TableColumn<Booking, String> tableColName;
    
    @FXML
    public TableColumn<Booking, Double> tableColHours;
    
    @FXML
    public TableColumn<Booking, String> tableColType;
    
//    @FXML
//    public TableColumn<Booking, String> tableColProj;
    
    @FXML
    public TableColumn<Booking, String> tableColDate;
    
    @FXML
    public TableColumn<Booking, String> tableColSTime;
    
    @FXML
    public TableColumn<Booking, String> tableColETime;
    
    
    
    
    int index = -1 ;
    

    
    
    @FXML
    public void initialize() throws IOException, ParseException {
        cycleComboBox.setValue("Täglich");
        cycleComboBox.setItems(intervalList);
        projectComboBox.setValue("Projekt Moderne Software Architekturen ");
        projectComboBox.setItems(projectTypeList);

       
        
        System.out.println("Inside initialize");
        
        tableColName.setCellValueFactory(new PropertyValueFactory<>("Name"));
        tableColDate.setCellValueFactory(new PropertyValueFactory<>("Date"));
        tableColSTime.setCellValueFactory(new PropertyValueFactory<>("startTime"));
        tableColETime.setCellValueFactory(new PropertyValueFactory<>("endTime"));
        tableColHours.setCellValueFactory(new PropertyValueFactory<>("Hours"));
        tableColType.setCellValueFactory(new PropertyValueFactory<>("Type"));

        tableViewBooking.setItems(getTableBookings());
        
      
        
        
    }

the problem here is that String is undefined type for Datepicker, it is LocalDate . So what to use here instead of .toString()?

beginDate.setValue(tableColDate.getCellData(index).toString());

CodePudding user response:

You should either make the tableColDate be TableColumn<Booking, LocalDate>. Then you can remove the toString() call and everything works. The reason is that setValue() expects a LocalDate, not a String.

Or you parse the date when you are setting the DatePicker: beginDate.setValue(LocalDate.parse(tableColDate.getCellData(index))).

  • Related