java - 带有cellfactory的javafx ComboBox不显示所选项目
问题描述
我有一个小的概念证明应用程序,其中包含 6 个Labels
aComboBox
和 a Button
,所有这些都是使用 SceneBuilder 创建的。
通过单击Button
,应用程序会调用 Json api 以返回国家列表及其相关详细信息(apla2code、apla3code、名称等)。我创建了一个CountryDetails
包含 3 个String
元素的对象。我使用它返回一个数组,CountryDetails
然后将其加载到ObserbavleList
. 然后我将它应用到 a 中ComboBox
,CountryDetails
每次在ComboBox
. 所有这一切都很好(尽管可能有更好的方法来做到这一点)。
我遇到的问题ComboBox
是没有显示所选项目,我无法弄清楚如何纠正这个问题。下图显示了问题所在。
调用 api 的代码如下:
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class GetCountries {
public CountryDetails[] getDetails() {
String inputLine = "";
StringBuilder jsonString = new StringBuilder();
HttpURLConnection urlConnection;
try {
URL urlObject = new URL("https://restcountries.eu/rest/v2/all");
urlConnection = (HttpURLConnection) urlObject.openConnection();
urlConnection.setRequestMethod("GET");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
while ((inputLine = bufferedReader.readLine()) != null) {
jsonString.append(inputLine);
}
urlConnection.getInputStream().close();
} catch(IOException ioe) {
System.out.println(ioe.getMessage());
}
Countries[] countries = new Gson().fromJson(jsonString.toString(), Countries[].class);
CountryDetails[] countryDetails = new CountryDetails[countries.length];
for(int i = 0; i < countries.length; i++){
countryDetails[i] = new CountryDetails(
countries[i].getAlpha2Code(),
countries[i].getAlpha3Code(),
countries[i].getName()
);
}
return countryDetails;
}
}
该CountryDetails
对象的代码如下:
public class CountryDetails {
private String alpha2Code;
private String alpha3Code;
private String name;
public CountryDetails(String strAlpha2Code, String strAlpha3Code, String strName) {
this.alpha2Code = strAlpha2Code;
this.alpha3Code = strAlpha3Code;
this.name = strName;
}
public String getAlpha2Code() { return alpha2Code; }
public void setAlpha2Code(String alpha2Code) { this.alpha2Code = alpha2Code; }
public String getAlpha3Code() { return alpha3Code; }
public void setAlpha3Code(String alpha3Code) { this.alpha3Code = alpha3Code; }
public String getName() { return name; }
public void setName(String name) {this.name = name; }
}
加载的代码ObservableList
如下:
GetCountries countries = new GetCountries();
CountryDetails[] countryDetails = countries.getDetails();
for (CountryDetails countryDetail : countryDetails) {
countriesObservableList.add(new CountryDetails(
countryDetail.getAlpha2Code(),
countryDetail.getAlpha3Code(),
countryDetail.getName())
);
}
加载ComboBox
和显示元素的代码Labels
如下:
cbCountryList.setCellFactory(new Callback<ListView<CountryDetails>, ListCell<CountryDetails>>() {
@Override public ListCell<CountryDetails> call(ListView<CountryDetails> p) {
return new ListCell<CountryDetails>() {
@Override
protected void updateItem(CountryDetails item, boolean empty) {
super.updateItem(item, empty);
if (empty || (item == null) || (item.getName() == null)) {
setText(null);
} else {
setText(item.getName());
}
}
};
}
});
public void comboAction(ActionEvent event) {
lblAlpha2Code.setText(cbCountryList.getValue().getAlpha2Code());
lblAlpha3Code.setText(cbCountryList.getValue().getAlpha3Code());
lblCountryName.setText(cbCountryList.getValue().getName());
}
下面是应用程序的图像:
解决方案
我遇到的问题是 ComboBox 没有显示所选项目,我无法弄清楚如何纠正这个问题。
您需要为您的cbCountryList
.
cbCountryList.setConverter(new StringConverter<CountryDetails>() {
@Override
public String toString(CountryDetails object) {
return object.getName();
}
@Override
public CountryDetails fromString(String string) {
return null;
}
});
所有这一切都很好(尽管可能有更好的方法来做到这一点)。
您可以考虑更新以下内容,
- 异步调用 HTTP 请求和加载项
fetched-country
每次触发按钮时,您都可以在调用时缓存列表。您可以为GetCountries
.
修改GetCountries
类,
它缓存国家列表并将缓存的数据用于多个请求,
public static class GetCountries {
private static final String API_URL = "https://restcountries.eu/rest/v2/all";
private static CountryDetails[] countryDetails;
public static CountryDetails[] getDetails() {
//uses cached countryDetails once it gets loaded
if (countryDetails != null) {
return countryDetails;
}
StringBuilder jsonString = new StringBuilder();
HttpURLConnection urlConnection;
try {
URL urlObject = new URL(API_URL);
urlConnection = (HttpURLConnection) urlObject.openConnection();
urlConnection.setRequestMethod(HttpMethod.GET.name());
String inputLine = "";
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
while ((inputLine = bufferedReader.readLine()) != null) {
jsonString.append(inputLine);
}
urlConnection.getInputStream().close();
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
Countries[] countries = new Gson().fromJson(jsonString.toString(), Countries[].class);
countryDetails = new CountryDetails[countries.length];
for (int i = 0; i < countries.length; i++) {
countryDetails[i] = new CountryDetails(
countries[i].getAlpha2Code(),
countries[i].getAlpha3Code(),
countries[i].getName()
);
}
return countryDetails;
}
}
使用Task异步获取您的国家/地区,
Task<CountryDetails[]> fetchCountryTask = new Task<CountryDetails[]>() {
@Override
protected CountryDetails[] call() throws Exception {
return GetCountries.getDetails();
}
};
fetchButton.setOnAction(event -> new Thread(fetchCountryTask).start());
fetchCountryTask.setOnRunning(event -> cbCountryList.setDisable(true));
fetchCountryTask.setOnSucceeded(e -> {
cbCountryList.getItems().addAll(fetchCountryTask.getValue());
cbCountryList.setDisable(false);
});
推荐阅读
- google-cloud-platform - 每次在 Google Cloud Storage 上上传 CSV 时如何触发自动更新 Google BigQuery 数据集
- reactjs - 如何在谷歌地图 API URL 中添加地点参数以获取特定类型的位置以进行自动完成
- python - 在特定的 MetaMessage 时间添加 MIDI 和弦
- typescript - React Native Animated 仅以一种方式工作?
- python - 您如何计算列表中某个模式的出现次数?
- javascript - 传递参数时出现GraphQL语法错误
- mongodb - 在 mongodb 中使用 geojson 进行分组
- linux - 原子分割文件
- node.js - 从未签入源代码控制的文件中加载 CodeBuild 环境变量
- c++ - 函数的内联定义 - 外部或内部链接