ฉันมีแอปพิสูจน์แนวคิดเล็กน้อยซึ่งประกอบด้วย 6 Labels
a ComboBox
และ Button
ซึ่งทั้งหมดสร้างขึ้นโดยใช้ SceneBuilder
เมื่อคลิก Button
แอปจะทำการเรียก Json api เพื่อส่งคืนรายชื่อประเทศและรายละเอียดที่เกี่ยวข้อง (apla2code, apla3code, ชื่อ ฯลฯ) ฉันสร้างวัตถุ CountryDetails
ซึ่งมี 3 String
องค์ประกอบ ฉันใช้สิ่งนั้นเพื่อส่งคืนอาร์เรย์ของ CountryDetails
ซึ่งฉันโหลดลงในอาร์เรย์ของ ObserbavleList
จากนั้นฉันใช้สิ่งนั้นกับ ComboBox
และฉันโหลดองค์ประกอบ CountryDetails
ลงใน 3 ป้ายกำกับแต่ละครั้งที่มีการเลือกรายการใน 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#buttonCell
เป็นอินสแตนซ์ ส่งกลับโดยโรงงานเซลล์ที่คุณกำหนดเอง - person Slaw   schedule 07.01.2020