0

I have started using JavaFX for a few weeks. Here is a code snap of a TableView

@FXML protected void handleSubmitButtonAction(ActionEvent event) {
    log.debug("handleSubmitButtonAction");
    service.save(new InputText(inputText.getText()));

    List<OutputData> outputDataList = new ArrayList<>();
    service.getAll().forEach(e -> outputDataList.add(new OutputData(e)));
    ObservableList<OutputData> list = FXCollections.observableArrayList(outputDataList);
    outputView.setItems(list);

    inputText.setText("");
}

What it does is that

  • Create a new entry
  • Save it to DB
  • Fetch all entries from DB
  • Create TableView row data for each entry
  • Create an ObservableList with all of the row data
  • Add the list to the TableView

That certainly isn't effective. Is a way to add a newly created entry directly to the TableView?

James_D
  • 201,275
  • 16
  • 291
  • 322
vic
  • 2,548
  • 9
  • 44
  • 74
  • 5
    Just create the row data only for the new entry and pass it to `outputView.getItems().add(…)`. Create and post a [mre] if you can’t get it to work. – James_D Sep 14 '22 at 19:44
  • Thanks. And interesting! That bypasses the data type designed for a row in the TableView and ObservableList. The property in the needs to match the data, but not the data type designed for the ViewTable. – vic Sep 14 '22 at 22:17
  • 5
    I don’t understand what you’re saying at all there. The table view is displaying `OutputData` objects. Assuming everything is set up correctly, the only thing you can pass to `outputView.getItems().add(…)` is an `OutputData` instance. You should not use `PropertyValueFactory` in modern applications: see https://stackoverflow.com/q/72437983/2189127 – James_D Sep 14 '22 at 23:46
  • What I tried to say is that the approach omits the data type, which its properties are the SimpleStringProperty type, defined for a row of the TableView. And ObservableList is not needed either (BTW what it is for originally?) A column in the FXML file is coded as: How to map the property if PropertyValueFactory isn't used? Sorry, I am a beginner for JavaFX. – vic Sep 15 '22 at 18:06
  • *"the approach omits the data type"*. This doesn't make sense. Java is strongly typed. Every object has a runtime type, every variable has a compile-time type and the types must match the expected parameter type at compile time. The `add(...)` method will require an object of type `OutputData`; if you pass the wrong type, it's a compile error. *"ObservableList is not needed either"*. No, it is absolutely needed. Look at the docs for the return type of `TableView.getItems()`. *"How to map the property if PropertyValueFactory isn't used?"* See link in previous comment. – James_D Sep 15 '22 at 18:29

1 Answers1

1

Here is what I would do.

  1. Have service.save(new InputText(inputText.getText())); return a true if the data was saved to the DB and false otherwise.

  2. If true is returned, I would then add the data to the TableView using outputView.getItems().add(inputText.getText());

  3. If false is retured, I would use an Alert to let the user know that the data was not added to the DB.

SedJ601
  • 12,173
  • 3
  • 41
  • 59
  • The output data isn't the same as the input text. I did something on the same track. – vic Sep 15 '22 at 18:08
  • 1
    Have a look at [this](https://github.com/sedj601/SQLitePersonTableViewExample). Hopefully, it will help. – SedJ601 Sep 16 '22 at 00:02