Eu tenho uma pequena prova de aplicativo conceito que contém 6 Labels
um ComboBox
e um Button
, todos criados usando SceneBuilder.
Ao clicar no Button
, o aplicativo faz uma chamada API JSON para retornar uma lista de países e seus detalhes relacionados (apla2code, apla3code, nome etc). Eu criei um CountryDetails
objeto que detém 3 String
elementos. Eu uso que para retornar uma matriz da CountryDetails
qual I depois carregar em uma matriz de ObserbavleList
. Em seguida, aplicam-se que para um ComboBox
e carregar os CountryDetails
elementos em 3 etiquetas de cada vez que um produto é seleccionado no ComboBox
. O Tudo isso funciona bem (embora provavelmente há uma maneira muito melhor de fazer isso).
O problema que estou tendo é que o ComboBox
não está exibindo o item selecionado e eu não consigo descobrir como corrigir isso. A imagem abaixo mostra qual é o problema.
O código que faz a chamada de API é a seguinte:
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;
}
}
O código para o CountryDetails
objecto é a seguinte:
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; }
}
O código que carrega a ObservableList
é a seguinte:
GetCountries countries = new GetCountries();
CountryDetails[] countryDetails = countries.getDetails();
for (CountryDetails countryDetail : countryDetails) {
countriesObservableList.add(new CountryDetails(
countryDetail.getAlpha2Code(),
countryDetail.getAlpha3Code(),
countryDetail.getName())
);
}
O código que carrega as ComboBox
e exibe os elementos do Labels
é a seguinte:
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());
}
Abaixo está a imagem do app:
O problema que estou tendo é que a caixa de combinação não está exibindo o item selecionado e eu não consigo descobrir como corrigir isso.
Você precisa definir um StringConverter para o seu cbCountryList
.
cbCountryList.setConverter(new StringConverter<CountryDetails>() {
@Override
public String toString(CountryDetails object) {
return object.getName();
}
@Override
public CountryDetails fromString(String string) {
return null;
}
});
O Tudo isso funciona bem (embora provavelmente há uma maneira muito melhor de fazer isso).
Você pode considerar atualizar as seguintes coisas,
- chamada assíncrona para o pedido HTTP e itens de carga
- Você pode armazenar em cache sua
fetched-country
lista como você está chamando cada vez que o botão é acionado. Você pode fazer um Singleton objeto paraGetCountries
.
Modificado GetCountries
classe ,
Ele armazena a lista de países e usa os dados em cache para vários pedidos,
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 para buscar seus países de forma assíncrona,
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);
});