我正在尝试创建一个ComboBox
,它将显示所选Image
的预览,但是ComboBox
显示的是字符串值。
唯一似乎有效的方法是创建Node
的ComboBox
,但是这会导致一旦选择了选项,该选项就会从下拉菜单中消失。 如有建议,敬请赐教。
我的代码如下:
String notOnLine = "file:Java1.png";
String onLine = "file:Java2.png";
ObservableList<String> options = FXCollections.observableArrayList();
options.addAll(notOnLine, onLine);
final ComboBox<String> comboBox = new ComboBox(options);
comboBox.setCellFactory(c -> new StatusListCell());
而且还有ListCell
:
public class StatusListCell extends ListCell<String> {
protected void updateItem(String item, boolean empty){
super.updateItem(item, empty);
setGraphic(null);
setText(null);
if(item!=null){
ImageView imageView = new ImageView(new Image(item));
imageView.setFitWidth(40);
imageView.setFitHeight(40);
setGraphic(imageView);
setText("a");
}
}
}
当下拉列表关闭后,我希望图像能够在ComboBox
本身中显示。目前它只显示URL(例如file:Java1.png
)。