Adding individual item to a TableView?

Viewed 49

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?

1 Answers

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.

Related