Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 311
Elias Elias Avatar asked Aug 09 '13 12:08

Elias Elias


People also ask

What is a combo box in JavaFX?

ComboBox is a part of the JavaFX library. JavaFX ComboBox is an implementation of simple ComboBox which shows a list of items out of which user can select at most one item, it inherits the class ComboBoxBase.

What is the difference between ComboBox and ChoiceBox JavaFX?

The JavaFX ComboBox control enables users to choose an option from a predefined list of choices, or type in another value if none of the predefined choices matches what the user want to select. The JavaFX ChoiceBox control enables users to choose an option from a predefined list of choices only.


4 Answers

    comboBox.getSelectionModel().select(indexOfItem);
or
    comboBox.setValue("item1");
like image 94
Capriatto Avatar answered Oct 16 '22 11:10

Capriatto


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.

like image 16
A J Qarshi Avatar answered Oct 16 '22 11:10

A J Qarshi


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));
like image 9
Brendan Avatar answered Oct 16 '22 10:10

Brendan


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"
like image 3
Matthes Avatar answered Oct 16 '22 10:10

Matthes