Search code examples
selectcomboboxjavafx-2

Combo-box select item in JavaFX 2


I have one [JavaFX] ComboBox that is populated with countries.

My object:

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

Then I have my ObservableList:

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

Then my ComboBox which displays the name of the country and the code of the country is for my own use:

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

How can I auto-select a country, for example Germany, if I only have the code of the country, which is "DE"?


Solution

  • Couple of months old question but here is more elegant solution for such type of problems.

    Modify the CountryObj class and override the hashCode and equals functions as below:

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

    Now in you code if you will execute the following statement, it will automatically select "Germany" for you.

    cmbCountry.getSelectionModel().select("DE")
    

    You can also pass an object of CountryObj to select method above.