Elias Elias
Elias Elias

Reputation: 429

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"?

Upvotes: 19

Views: 80600

Answers (5)

Matthes
Matthes

Reputation: 31

If both comboBox are from the same Array, assembly column one and two, then they have the same sequence. Then you can use the index.

a = comboBox1.getSelectionModel().getSelectedIndex(); 
comboBox2.getSelectionModel().select(a);

"United States" is on index position 1 "US" also on index position 1 then:

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

Upvotes: 3

michael laudrup
michael laudrup

Reputation: 27

The solution of Brendan and Branislav Lazic is perfect, but we still can improve the call to the autoSelectComboBoxValue method :

public static <T, V> void autoSelectComboBoxValue(ComboBox<T> comboBox, V value, Func<T, V> f) {...}

:)

Upvotes: 0

Brendan
Brendan

Reputation: 3649

I think the simplest solution is to write an autoSelect function that finds the matching CountryObj in your ObservableList. Once you find the correct CountryObj, tell the combobox to set that as its value. It should looks something like this...

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

EDIT:

This can be further refactored to reusable method for all ComboBox'es that take different type parameter:

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

where Func is an interface:

public interface Func<T, V> {
    boolean compare(T t, V v);
}

How to apply this method:

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

Upvotes: 9

Capriatto
Capriatto

Reputation: 965

    comboBox.getSelectionModel().select(indexOfItem);
or
    comboBox.setValue("item1");

Upvotes: 23

A J Qarshi
A J Qarshi

Reputation: 2992

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.

Upvotes: 17

Related Questions