带有 cellfactory 的 javafx ComboBox 不显示所选项目

标签 java javafx combobox observablelist

我有一个小型概念验证应用程序,其中包含 6 个 Labels、一个 ComboBox 和一个 Button,所有这些都是使用 SceneBuilder 创建的。

通过单击按钮,应用程序会进行 Json API 调用以返回国家/地区列表及其相关详细信息(apla2code、apla3code、名称等)。我创建了一个 CountryDetails 对象,其中包含 3 个 String 元素。我使用它返回一个 CountryDetails 数组,然后将其加载到 ObserbavleList 数组中。然后,我将其应用于 ComboBox,并且每次在 ComboBox 中选择一个项目时,我都会将 CountryDetails 元素加载到 3 个标签中。所有这些都工作得很好(尽管可能有更好的方法来做到这一点)。

我遇到的问题是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());
    }

下面是应用程序的图像:

enter image description here

最佳答案

The problem I am having is that the ComboBox is not displaying the selected item and I cannot figure out how to correct this.

您需要设置StringConverter为您的cbCountryList

cbCountryList.setConverter(new StringConverter<CountryDetails>() {
    @Override
    public String toString(CountryDetails object) {
        return object.getName();
    }

    @Override
    public CountryDetails fromString(String string) {
        return null;
    }
});

The All of this works fine (although there is probably a much better way of doing this).

您可以考虑更新以下内容,

  • 异步调用 HTTP 请求并加载项目
  • 每次触发按钮时,您都可以在调用时缓存您的fetched-country列表。你可以做一个Singleton 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);
});

关于带有 cellfactory 的 javafx ComboBox 不显示所选项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59628293/

相关文章:

C# 如何从 WinForms 中的 ComboBox 获取枚举值?

WPF:在 XAML 中更改 ComboBox 箭头按钮颜色

java - 返回平方数的 intStream

java - 比较两个列表的内容,Java,Selenium

java - JavaFX 属性对象对于多个异步写入来说是线程安全的吗?

.net - 绑定(bind) ComboBox 双向模式不起作用?

java - 如何使用java中的Scanner来计算输入.txt文件中以 ","分隔的单词数?

java - Android - 带有自定义 CursorAdapter 的 Listview,运行异步任务会崩溃

java - 如何删除 StackPane 大小的额外中断?

java - 为什么没有单击正确的 TextField TestFX?