javafx ComboBox with cellfactory does not display selected item

Yasterfari :

I have a small proof of concept app which contains 6 Labels a ComboBox and a Button, all created using SceneBuilder.

By clicking the Button, the app makes a Json api call to return a list of countries and their related details (apla2code, apla3code, name etc). I created a CountryDetails object which holds 3 String elements. I use that to return an array of the CountryDetails which I then load into an array of ObserbavleList. I then apply that to a ComboBox and I load the CountryDetails elements into 3 labels each time an item is selected in the ComboBox. The All of this works fine (although there is probably a much better way of doing this).

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

The code which makes the api call is as follows:

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;
    }
} 

The code for the CountryDetails object is as follows:

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; }
}

The code which loads the ObservableList is as follows:

GetCountries countries = new GetCountries();

        CountryDetails[] countryDetails = countries.getDetails();

        for (CountryDetails countryDetail : countryDetails) {
            countriesObservableList.add(new CountryDetails(
                    countryDetail.getAlpha2Code(),
                    countryDetail.getAlpha3Code(),
                    countryDetail.getName())
            );
        }

The code which loads the ComboBox and displays the elements in the Labels is as follows:

    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());
    }

Below is the image of the app:

enter image description here

Shekhar Rai :

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

You need to set a StringConverter for your 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).

You may consider updating the following things,

  • Asynchronous call for HTTP request and load items
  • You can cache your fetched-country list as you're calling every time when the button gets triggered. You can make a Singleton object for GetCountries.

Modified GetCountries class,

It caches the countries list and uses the cached data for multiple requests,

 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;
    }
}

Use Task to fetch your countries asynchronously,

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);
});

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=359564&siteId=1