-1

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 onMouseClicked 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());
  • 2
    You should be getting data from the model, not from the table columns. Create and post a [mre]. – James_D Nov 01 '22 at 17:50
  • 2
    Surely `tableColDate` should be a `TableColumn`? – James_D Nov 01 '22 at 17:51
  • No, for some other reason I convert LocalDate to String when I send my Datepicker Value to the TableView – Cicada3301 Nov 01 '22 at 18:09
  • 2
    Why? That makes no sense. If it's a date, store it as a `LocalDate`. – James_D Nov 01 '22 at 18:22
  • 2
    This is just wrong. You create a model class, `Booking`, specifically to store the data for the rows in the table. It makes no sense at all to store the date as a string in a class specifically designed only for storing data.. You have no way of checking in that class that valid dates are stored, and as you're discovering here, you then have to parse them to use them as dates. What is the point of all this wasteful and error-prone parsing? – James_D Nov 01 '22 at 18:29
  • it gives me this error java.lang.reflect.InaccessibleObjectException: Unable to make field private final int java.time.LocalDate.year accessible: module java.base does not "opens java.time" to unnamed module @1d76e064. It has to something with my code that connect to the db. Right here Booking booking = gson.fromJson(responseObject.toString(), Booking.class); its the toString again. But its a whole another issue. I probably should figure it out but its a whole another problem – Cicada3301 Nov 01 '22 at 19:12
  • See if the ideas from [this](https://stackoverflow.com/questions/70302119/show-json-in-tableview/70302970#70302970) can help you solve your `gson` issue. – SedJ601 Nov 01 '22 at 19:24
  • Don't pollute your data model because you can't be bothered to make it work with your persistence engine. There are lots of resources on making GSON work with `LocalDate`, e.g. https://www.javaguides.net/2019/11/gson-localdatetime-localdate.html. (Generally I favor Jackson over GSON as I think it understands the whole concept of encapsulation better, but that is just a preference, I suppose.) – James_D Nov 01 '22 at 19:32
  • merci!! let me try :) – Cicada3301 Nov 01 '22 at 20:09

1 Answers1

-1

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))).

Maran23
  • 226
  • 2
  • 9
  • thankyou parsing the date works – Cicada3301 Nov 01 '22 at 18:17
  • 2
    Just as a little side note here, you still should lookup and learn more about types and especially how to handle them. James_D is right here, the better solution in the long run would be to adjust the column so it fits the model. :) – Maran23 Nov 01 '22 at 18:55