我的應用程序使用線程定期將字符串添加到列表。我想將這些字符串添加到列表後,立即將這些字符串添加到組合框。反正有沒有把組合框綁定到列表?如何將ComboBox綁定到JavaFX中的列表?
我的代碼:
static final int max = 20;
List<String> ips = new ArrayList<String>();
public void getIP() throws UnknownHostException {
Task task = new Task<Void>() {
@Override
public Void call() throws UnknownHostException {
InetAddress localhost = InetAddress.getLocalHost();
byte[] ip = localhost.getAddress();
for (int i = 10; i <= max; i++) {
if (isCancelled()) {
break;
}
try {
ip[3] = (byte) i;
InetAddress address = InetAddress.getByAddress(ip);
if (address.isReachable(100)) {
//============================== Populating List ===============//
ips.add(address.getHostName());
}
} catch (Exception e) {
System.err.println(e);
}
updateProgress(i, max);
}
return null;
}
};
//============================== Bind ComboBox to List Code here ===============//
indicator.progressProperty().bind(task.progressProperty());
new Thread(task).start();
}
我用Platform.runLater和它的作品。然而,我有一個奇怪的問題,如果我在填充它時打開了ComboBox,它一次只顯示一個可見項目,並將其他項目添加到scrolldown。但是,如果我在完成線程後打開組合框,它將顯示所有項目而沒有滾動顯示。 –