2013-08-09 34 views
12

Tôi có một kết hợp trong javaFX có dân cư với các quốc gia.Mục chọn hộp kết hợp trong JavaFX 2

đối tượng của tôi:

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

}  

Sau đó, tôi có thể quan sát được danh sách của tôi:

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

Sau đó combo của tôi. Rằng tên trên đất nước có thể nhìn thấy và tôi có thể có mã của đất nước để sử dụng riêng của tôi:

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

Làm thế nào tôi có thể tự động chọn ví dụ Đức. Nếu tôi chỉ biết mã của đất nước? "DE"

Trả lời

8

Vài tháng câu hỏi cũ nhưng đây là giải pháp thanh lịch hơn cho loại hình như vậy vấn đề.

Sửa đổi các lớp CountryObj và ghi đè lên các chức năng hashCodeequals như sau:

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

Bây giờ trong bạn mã nếu bạn sẽ thực hiện các tuyên bố sau, nó sẽ tự động chọn "Đức" cho bạn.

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

Bạn cũng có thể chuyển đối tượng của CountryObj đến select ở trên.

6

Tôi nghĩ giải pháp đơn giản nhất là viết hàm autoSelect tìm thấy kết hợp CountryObj trong ObservableList của bạn. Khi bạn tìm thấy chính xác CountryObj, hãy yêu cầu hộp kết hợp đặt giá trị đó làm giá trị của nó. Nó nên trông giống như thế này ...

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

EDIT:

Điều này có thể được tiếp tục refactored phương pháp tái sử dụng cho tất cả ComboBox'es rằng lấy tham số kiểu khác nhau:

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

nơi Func là một giao diện:

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

Làm thế nào để áp dụng phương pháp này:

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

Tôi đã sử dụng đề nghị của bạn và nó là perfer làm việc. Cảm ơn nhiều. –

+0

Giải pháp tốt nhất cho đến nay. –

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

Các giải pháp của Brendan và Branislav Lazic là hoàn hảo, nhưng chúng tôi vẫn có thể cải thiện cuộc gọi đến phương pháp autoSelectComboBoxValue:

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

:)

0

Nếu cả hai ComboBox là so với cùng Mảng , lắp ráp cột một và hai, sau đó họ có cùng một trình tự. Sau đó, bạn có thể sử dụng chỉ mục.

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

"Hoa Kỳ" là vào vị trí chỉ số 1 "Hoa Kỳ" cũng vào vị trí chỉ số 1 thì:

comboBox2.getSelectionModel().select(1); // is "US" 
Các vấn đề liên quan