Select JavaFX2中的组合框选择项

Select JavaFX2中的组合框选择项,select,combobox,javafx-2,Select,Combobox,Javafx 2,我有一个[JavaFX]组合框,里面填充了国家 我的目标: public static class CountryObj { private String TCountryDescr; private String TCountryCode; private CountryObj(String CountryDescr,String CountryCode) { this.TCountryDescr = CountryDescr;

我有一个[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;
    }
}    
然后我有我的
观察列表

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

如果我只有一个国家的代码,即“DE”,我如何自动选择一个国家(例如德国)?

我认为最简单的解决方案是编写一个自动选择函数,在您的ObservableList中查找匹配的CountryObj。找到正确的CountryObj后,告诉combobox将其设置为其值。看起来应该是这样的

private void autoSelectCountry(String countryCode)
{
    for (CountryObj countryObj : countryComboList)
    {
        if (countryObj.getTCountryCode().equals(countryCode))
        {
            cbCountry1.setValue(countryObj);
        }
    }
}
编辑:

对于所有采用不同类型参数的
组合框
,这可以进一步重构为可重用的方法:

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);
        }
    }
}
如何应用此方法:

autoSelectComboBoxValue(comboBox, "Germany", (cmbProp, val) -> cmbProp.getNameOfCountry().equals(val));

几个月前的问题,但这里有更优雅的解决此类问题的方法

修改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;
  }    
}
现在在您的代码中,如果您执行以下语句,它将自动为您选择“德国”


您还可以将CountryObj对象传递给上面的
选择方法。

Brendan和Branislav Lazic的解决方案非常完美,但我们仍然可以改进对AutoSelectComboxValue方法的调用:

    comboBox.getSelectionModel().select(indexOfItem);
or
    comboBox.setValue("item1");
public static <T, V> void autoSelectComboBoxValue(ComboBox<T> comboBox, V value, Func<T, V> f) {...}
publicstaticvoid autoselectcomboxvalue(ComboBox-ComboBox,V-value,Func-f){…}

:)

如果两个组合框都来自同一个数组、程序集列1和列2,则它们具有相同的序列。然后可以使用索引

a = comboBox1.getSelectionModel().getSelectedIndex(); 
comboBox2.getSelectionModel().select(a);
“美国”位于指数位置1“美国”也位于指数位置1,然后:

comboBox2.getSelectionModel().select(1); // is "US"

我已经使用了你的建议,而且效果更好。非常感谢。这是迄今为止最好的解决方案。
public static <T, V> void autoSelectComboBoxValue(ComboBox<T> comboBox, V value, Func<T, V> f) {...}
a = comboBox1.getSelectionModel().getSelectedIndex(); 
comboBox2.getSelectionModel().select(a);
comboBox2.getSelectionModel().select(1); // is "US"