Eli*_*ias 16 select combobox javafx-2
我在javaFX中有一个用国家填充的组合.
我的目标:
public static class CountryObj {
private String TCountryDescr;
private String TCountryCode;
private CountryObj(String CountryDescr,String CountryCode) {
this.TCountryDescr = CountryDescr;
this.TCountryCode = CountryCode;
}
public String getTCountryCode() {
return TCountryCode;
}
public void setTCountryCode(String fComp) {
TCountryCode= fComp;
}
public String getTCountryDescr() {
return TCountryDescr;
}
public void setCountryDescr(String fdescr) {
TCountryDescr = fdescr;
}
@Override
public String toString() {
return TCountryDescr;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有我的可观察列表:
private final ObservableList<CountryObj> CountrycomboList =
FXCollections.observableArrayList(
new CountryObj ("United States", "US"),
new CountryObj ("United Kingdom", "UK"),
new CountryObj ("France", "FR"),
new CountryObj ("Germany", "DE"));
Run Code Online (Sandbox Code Playgroud)
然后我的组合.这个国家的名字是可见的,我可以拥有该国家的代码供我自己使用:
private ComboBox<CountryObj> cCountry1 = new ComboBox<>();
cbCountry1.setItems(CountrycomboList);
cbCountry1.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<CountryObj>() {
@Override
public void changed(ObservableValue<? extends CountryObj> arg0, CountryObj arg1, CountryObj arg2) {
if (arg2 != null) {
System.out.println("Selected Country: " + arg2.getTCountryCode());
}
}
});
Run Code Online (Sandbox Code Playgroud)
我怎样才能自动选择德国.如果我只知道国家的代码?"DE"
Jua*_*ina 16
comboBox.getSelectionModel().select(indexOfItem);
or
comboBox.setValue("item1");
Run Code Online (Sandbox Code Playgroud)
A J*_*shi 12
几个月的问题,但这里是这种类型的问题更优雅的解决方案.
修改CountryObj类并覆盖hashCode和equals函数如下:
public class CountryObj {
private String TCountryDescr;
private String TCountryCode;
public CountryObj(String CountryDescr,String CountryCode) {
this.TCountryDescr = CountryDescr;
this.TCountryCode = CountryCode;
}
public String getTCountryCode() {
return TCountryCode;
}
public void setTCountryCode(String fComp) {
TCountryCode= fComp;
}
public String getTCountryDescr() {
return TCountryDescr;
}
public void setCountryDescr(String fdescr) {
TCountryDescr = fdescr;
}
@Override
public String toString() {
return TCountryDescr;
}
@Override
public int hashCode() {
int hash = 0;
hash += (TCountryCode != null ? TCountryCode.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
String otherTCountryCode = "";
if (object instanceof Country) {
otherTCountryCode = ((Country)object).TCountryCode;
} else if(object instanceof String){
otherTCountryCode = (String)object;
} else {
return false;
}
if ((this.TCountryCode == null && otherTCountryCode != null) || (this.TCountryCode != null && !this.TCountryCode.equals(otherTCountryCode))) {
return false;
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
现在在您的代码中,如果您将执行以下语句,它将自动为您选择"德国".
cmbCountry.getSelectionModel().select("DE")
Run Code Online (Sandbox Code Playgroud)
您还可以将CountryObj的对象传递给select上面的方法.
我认为最简单的解决方案是编写一个autoSelect函数,在ObservableList 中找到匹配的CountryObj.找到正确的CountryObj后,告诉组合框将其设置为其值.它应该看起来像这样......
private void autoSelectCountry(String countryCode)
{
for (CountryObj countryObj : countryComboList)
{
if (countryObj.getTCountryCode().equals(countryCode))
{
cbCountry1.setValue(countryObj);
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
对于所有ComboBox'es采用不同类型参数的方法,这可以进一步重构为可重用的方法:
public static <T> void autoSelectComboBoxValue(ComboBox<T> comboBox, String value, Func<T, String> f) {
for (T t : comboBox.getItems()) {
if (f.compare(t, value)) {
comboBox.setValue(t);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Func接口在哪里:
public interface Func<T, V> {
boolean compare(T t, V v);
}
Run Code Online (Sandbox Code Playgroud)
如何应用此方法:
autoSelectComboBoxValue(comboBox, "Germany", (cmbProp, val) -> cmbProp.getNameOfCountry().equals(val));
Run Code Online (Sandbox Code Playgroud)