I am a new in JavaFX and I am trying to update ChoiceBox when new device like flash card was connected. I do it using ScheduleService which has period 500ms. After each 500ms I get a list of removable devices and add them to ObservableList:
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
ScheduledService<List<String>> service = getListOfDevices();
service.setPeriod(Duration.millis(500));
service.start();
service.setOnSucceeded(e -> {
Platform.runLater(() -> flashCardObservableList.setAll(service.getLastValue()));
});
nckCardList.setItems(flashCardObservableList);
}
private ScheduledService<List<String>> getListOfDevices() {
return new ScheduledService<List<String>>() {
@Override
protected Task<List<String>> createTask() {
return new Task<List<String>>() {
@Override
protected List<String> call() throws Exception {
String result = usb.GetDriveInfo();
List<FlashCard.DriveInfo> drives;
List<String> diskLetters = new ArrayList<>();
if (result.isEmpty() || result == null) return diskLetters;
drives = FlashCard.deserialize(result);
for (FlashCard.DriveInfo d : drives) {
diskLetters.add(d.getFlashName());
}
return diskLetters;
}
};
}
};
}
For getting list of devices I am using DLL and in this DLL I use method DriveInfo.GetDrives(). But every 500ms my choiceBox is updating and I cannot pick anything. I want it to look like in the program "Rufus" in the field - "Device". What is the best way to do so?