ListView doesn't update JavaFx
In the Code below, the ListView called tourList doesn't get updated eventhough the Observable List tourNameList has the updated data. When I start the program, the initial list is shown with the DB-entries, but when I add a new entry, no update is made. Can someone help...
public class TourListView implements TourListObserver {
public TourListViewModel tourListViewModel = new TourListViewModel();
public TourFormViewModel tourFormViewModel = new TourFormViewModel();
public FormInputManager formInputManager = new FormInputManager();
Alert alert = new Alert(Alert.AlertType.ERROR);
Alert successPrompt = new Alert(Alert.AlertType.INFORMATION);
//Form Components
@FXML
public ScrollPane tourForm;
@FXML
public TextField formFrom;
public ChoiceBox<String> formTransportType = new ChoiceBox<>();
public TextField formTo;
@FXML
public TextField formName;
@FXML
public TextField formDescription;
@FXML
public Button formSubmitButton;
@FXML
public ListView<String> tourList = new ListView<>();
public TextArea formRouteInformation;
@Override
public void onTourListUpdated() {
Platform.runLater(this::updateTourList); //thread safe update
}
public void updateTourList() {
//check if I am creating a second List
System.out.println("UPDATE TOUR LIST: TourListView");
ObservableList<String> tourNameList = tourListViewModel.getTourNameList();
System.out.println("tourNameList: " + tourNameList.toString() ); //tourNameList has shows new Data
this.tourList.setItems(tourNameList); //this doesn't show new data
//this.tourList.getItems().setAll(tourNameList);
//this.tourList.refresh();
}
@FXML
public void addTour(ActionEvent event) {
System.out.println("Button clicked: addTour");
//validate Input
FormTour formTour = new FormTour(formName.getText(), formDescription.getText(), formFrom.getText(), formTo.getText(), formTransportType.getValue(), formRouteInformation.getText());
String validationString = formInputManager.validateForm(formTour);
if (Objects.equals(validationString, FormMessages.VALID_FORM.getMessage())) {
//post them to DB
System.out.print("VALID INPUT");
System.out.printf(validationString);
tourFormViewModel.postTour(formTour);
tourListViewModel.updateList();
successPrompt.setTitle("Tour has been added");
successPrompt.setContentText(FormMessages.FORM_ADDED.getMessage());
successPrompt.setHeaderText("");
successPrompt.showAndWait();
} else {
//show error message
alert.setTitle("INVALID INPUT");
alert.setContentText(validationString);
alert.setHeaderText("");
alert.showAndWait();
System.out.printf(validationString);
}
}
public void addTourFormShow(ActionEvent event) {
//make TourList invisible
if (tourList.isVisible()) {
tourList.setVisible(false);
tourForm.setVisible(true);
} else {
tourList.setVisible(true);
tourForm.setVisible(false);
}
System.out.println("Button clicked: startForm");
}
@javafx.fxml.FXML
public void initialize() {
tourListViewModel.registerObserver(this);
//init List
ObservableList<String> tourNameListList = FXCollections.observableArrayList();
tourNameListList = tourListViewModel.getTourNameList();
this.tourList.setItems(tourNameListList);
//init ChoiceBox
String[] choiceBoxChoices = {"public transit", "car", "bike", "foot"};
formTransportType.getItems().addAll(choiceBoxChoices);
}
}
I tried
this.tourList.getItems().setAll(tourNameList);
this.tourList.refresh();
but it also didn't work
As requested the FXML file:
<SplitPane dividerPositions="0.10738255033557047"
orientation="VERTICAL"
prefHeight="300.0"
prefWidth="174.0"
AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0"
fx:controller="at.fhtw.app.view.TourListView"
xmlns="http://javafx.com/javafx/8.0.171"
xmlns:fx="http://javafx.com/fxml/1">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="49.0" prefWidth="172.0"
SplitPane.resizableWithParent="false">
<children>
<Label layoutY="5.0" text="Tours"/>
<Button fx:id="addButton" onAction="#addTourFormShow" layoutX="94.0" layoutY="1.0"
mnemonicParsing="false" text="+" AnchorPane.rightAnchor="51.0"
AnchorPane.topAnchor="0.0"/>
<Button layoutX="121.0" layoutY="1.0" mnemonicParsing="false" text="-" AnchorPane.rightAnchor="27.0"
AnchorPane.topAnchor="0.0"/>
<Button layoutX="145.0" layoutY="1.0" mnemonicParsing="false" text="..." AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0"/>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<fx:include fx:id="tourForm" source="Tour-Form.fxml" visible="false"/>
<ListView fx:id="tourList" prefHeight="262.0" prefWidth="172.0" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"/>
</children>
</AnchorPane>
</items>
</SplitPane>