问题描述
是否有一种方法来填充枚举的所有枚举的JavaFX ComboBox
或 ChoiceBox
? >
这是我试过的:
public class Test {
public enum Status {
ENABLED(enabled),
DISABLED(disabled),
UNDEFINED(undefined);
private String label;
Status(String label){
this.label = label;
}
public String toString(){
return label;
}
}
}
'm尝试填充 ComboBox
:
ComboBox< Test.Status> ; cbxStatus = new ComboBox<>();
cbxStatus.setItems(Test.Status.values());
但是我收到一个错误:不兼容的类型:Status []无法转换到ObservableList< Status>
我显然遇到了与 ChoiceBox
相同的问题。
如果setItems需要一个ObservableList,那么你必须给它一个而不是一个数组。
试试这个:
ComboBox< Status> cbxStatus = new ComboBox<>();
cbxStatus.setItems(FXCollections.observableArrayList(Status.values()));
编辑:James_D(请参阅评论)的解决方案是首选:
cbxStatus.getItems()。setAll(Status.values());
Is there a way to populate a JavaFX ComboBox
or ChoiceBox
with all enumerations of a enum ?
Here is what I tried :
public class Test {
public enum Status {
ENABLED("enabled"),
DISABLED("disabled"),
UNDEFINED("undefined");
private String label;
Status(String label) {
this.label = label;
}
public String toString() {
return label;
}
}
}
In a another class, I'm trying to populate a ComboBox
:
ComboBox<Test.Status> cbxStatus = new ComboBox<>();
cbxStatus.setItems(Test.Status.values());
But I get an error : incompatible types: Status[] cannot be converted to ObservableList<Status>
I obviously get the same problem with a ChoiceBox
.
If setItems requires an ObservableList, then you have to give it one instead of an array.
Try this:
ComboBox<Status> cbxStatus = new ComboBox<>();
cbxStatus.setItems( FXCollections.observableArrayList( Status.values()));
Edit: The solution of James_D (see comment) is the preferred one:
cbxStatus.getItems().setAll(Status.values());
这篇关于从枚举填充JavaFX ComboBox或ChoiceBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!