如何设置对象的属性值以显示选择框?
@FXML
private ChoiceBox<LuggageBrand> brandSelector;
在这里您可以看到我用 LuggageBrand
对象填充选择框,但现在应用程序上显示的值是 .toString()
的对象。
我该怎么说,嘿,请使用此属性在选择框上显示?
最佳答案
在ChoiceBox
上设置转换器:
public void initialize() {
brandSelector.setConverter(new StringConverter<LuggageBrand>() {
@Override
public String toString(LuggageBrand luggageBrand) {
// whatever logic you need here to turn the LuggageBrand object to a string:
String value = luggageBrand.getXXX();
return value ;
}
@Override
public LuggageBrand fromString(String text) {
// this method is not used by the ChoiceBox, so you can just
return null ;
}
});
// other initialization code...
}
关于java - 从数据库外的对象设置Choisebox的值属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41062233/