0

I have a static List in Config Class

public class Config {
    public static ObservableList<Device> OnlineDevicesList = FXCollections.observableArrayList();
}

when the List got changed, i need to change the Items in

public class MainViewController {
    @FXML ListView<Device> lvOnlineDevices;
    public void initialize() {
        lvOnlineDevices.getItems().addAll(Config.OnlineDevicesList);
    }
}

how can i do that?

Mr. 0x50
  • 314
  • 3
  • 14
  • There are better ways to get data to a controller than using `static` see this answer: http://stackoverflow.com/a/14190310/2991525 – fabian Feb 05 '16 at 10:52

1 Answers1

1

try

lvOnlineDevices.setItems(FXCollections.observableArrayList(OnlineDevicesList))

But I would suggest to use an ObservableList in your config, then you don't have to convert it evertime you have new items.

Raphael Roth
  • 26,751
  • 15
  • 88
  • 145
  • i updated my post, too. You can see that the list is now already an observableArrayList. But its not automatically updating – Mr. 0x50 Feb 05 '16 at 10:19
  • I mean using setItems() instead of getItems().addAll() – Raphael Roth Feb 05 '16 at 10:20
  • Ok, but when a second Item get added, i'll become an exception Exception in thread "Thread-3" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-3 – Mr. 0x50 Feb 05 '16 at 10:23
  • yes, because you cannot updated the GUI from a non-GUI thread. Either change your code and make sure change the list only in the GUI thread, or wrap the corresponding command in Platform.runlater(Runnable runnable) – Raphael Roth Feb 05 '16 at 10:33