Kenta1561
Kenta1561

Reputation: 29

ListView.refresh() does not work

somehow, although stated in the JavaDoc, the method refresh() does not work for me, only by calling setItems() again, which takes a lot of time especially when the associated ObservableList is long, works for me. This is my source code:

NetworkManager class, getStations() method

ObservableList<Station> getStations() {
        ObservableList<Station> stations = FXCollections.observableArrayList();
        JsonArray jsonArray = getRootElement().getAsJsonObject().getAsJsonArray("stations");
        for(JsonElement currentElement : jsonArray) {
            stations.add(gson.fromJson(currentElement, Station.class));
        }
        return stations;
}

Then I build up a connection between the ListView and this method in my JavaFX initialization method:

listView.setItems(networkManager.getStations());

Now, if I register a new object of Station and I use

listView.refresh();

the ListView does not get refreshed, only this one works:

listView.setItems(networkManager.getStations());

What is wrong here?

Kenta1561

EDIT: My Application-class extending main class:

package de.kenta1561.enr.main.java;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.TabPane;
import javafx.stage.FileChooser;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.Window;

import java.io.File;
import java.io.IOException;

public class EasyNetworkRegistration extends Application {

private File file;
private Controller controller;

private boolean fileCreated;

@Override
public void start(Stage primaryStage) throws Exception {
    showFileChooser(primaryStage);
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/de/kenta1561/enr/main/resources/EasyNetworkRegistration.fxml"));
    TabPane pane = loader.load();
    controller = loader.getController();
    initializeController();
    Scene scene = new Scene(pane);
    primaryStage.setScene(scene);
    primaryStage.setTitle("EasyNetworkRegistration");
    primaryStage.show();
    setCenter(primaryStage);
}

How I add a station to my JSON file:

void addStation(Station station) {
        JsonObject rootObject = getRootElement().getAsJsonObject();
        JsonArray jsonArray = rootObject.getAsJsonArray("stations");
        jsonArray.add(gson.toJsonTree(station));
        writeToFile(rootObject);
}

PS: Obviously the refresh() method gets called after this method gets called so that should not be the issue, I assume.

Upvotes: 0

Views: 1413

Answers (1)

James_D
James_D

Reputation: 209438

The only thing your addStation method does is update the JSON file. There's no way the table is going to know that anything has changed. The call to refresh() merely reloads the data from the same ObservableList, which, again, you haven't changed.

Consider adding the new station to the observable list directly:

void addStation(Station station) {
        JsonObject rootObject = getRootElement().getAsJsonObject();
        JsonArray jsonArray = rootObject.getAsJsonArray("stations");
        jsonArray.add(gson.toJsonTree(station));
        writeToFile(rootObject);
        listView.getItems().add(station);
}

(Refactor this in the obvious way if addStation is not in your controller.)

There should be no need to call refresh().

Upvotes: 3

Related Questions