Javafx newbie here.. I need help with the best way to populate listview. Here is my setup..
I am developing UI tool that is supposed to track the number of virtual machines running in my environment. I get a callback whenever a machine comes up or goes down. How do I update listview based on that data. Controller code -
public class MainOverviewController implements Initializable
{
@FXML
private ListView<String> devicesListView; // Points to the listview
@Override
public void initialize(URL location, ResourceBundle resources) {
ObservableList<String> items = FXCollections.observableArrayList("Machines connected");
devicesListView.setItems(items);
...
}
Callback code where I am getting the virtual machine notifications -
class VMChangeListener extends vmlistener
{
...
@Override
public void vmStarted(VM vm)
{
vms.add(vm);
}
@Override
public void vmDisconnected(VM vm)
{
vms.remove(vm);
}
Now my question is, whats the best way to update observablelist, items, from vmStarted and vmDisconnected functions. I could pass the observablelist to the VMChangeListener or use some sort of callbacks? Should I do this in seperate thread?
public class MainOverviewController extends vmListener implements Initializable
{
@FXML
private ListView<VM> devicesListView; // Points to the listview
@Override
public void vmStarted(final VM vm)
{
Platform.runLater(new Runnable()
{
@Override
public void run()
{
devicesListView.getItems().add(vm);
}
});
}
@Override
public void vmDisconnected(final VM vm)
{
Platform.runLater(new Runnable()
{
@Override
public void run()
{
devicesListView.getItems().remove(vm);
}
});
}
...
}