在我的项目中,我有一张桌子。当用户双击该行时,将打开一个编辑对话框。在打开此对话框时,我将值设置为字段,其中很少是ChoiceBoxes。 ChoiceBox的字段类型是自定义对象,而不是字符串。
创建ChoiceBox的代码如下:
trade_point.setConverter(new TradePointConverter());
trade_point.setItems(FXCollections.observableArrayList(tradePointsService.getTradePoints()));
TradePoint currentTradePoint = tradePointsService.getTradePoint(typeOfPriceByTradePoint.getTradePoint());
trade_point.setValue(currentTradePoint);
其中trade_point是TradePoint类型的选择框。 currentTradePoint不为null,当我查看trade_point的值等于currentTradePoint的值时,但在对话框中我看不到任何值。项目设置正确。在其他情况下,相同的选择框填写一切都是正确的,这里不是。
UPD: TradePointConverter类:
class TradePointConverter extends StringConverter<TradePoint>{
public TradePoint fromString(String name){
try {
List<TradePoint> tradePoints = tradePointsService.getTradePoints();
for (TradePoint tradePoint1: tradePoints){
if (tradePoint1.getName().equals(name)){
return tradePoint1;
}
}
}catch (SQLException e){
}
return null;
}
public String toString(TradePoint tradePoint1){
return tradePoint1.getName();
}
}
我不确定这个转换器是否正确,只要按字符串转换是完全错误的。我不明白如何让用户正确地看到它(查看对象的名称)以及如何存储其id以同时获取明确的对象。我可以通过获取id来转换为字符串,但在这种情况下,用户不会理解要选择的对象。
trade_point是一个ChoiceBox:
private ChoiceBox<TradePoint> trade_point = new ChoiceBox<TradePoint>();