gee*_*eef 4 java combobox javafx
我正在尝试创建一个ComboBox将显示 selected 的预览Image,但ComboBox显示字符串值。
唯一有效的方法似乎是创建ComboBoxof Node,但这会导致一旦选定的选项从下拉菜单中消失,如果有人有任何建议,我将不胜感激。
我的代码如下:
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());
Run Code Online (Sandbox Code Playgroud)
和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");
}
}
}
Run Code Online (Sandbox Code Playgroud)

ComboBox我希望在列表关闭后图像本身显示出来。现在它只显示 URL(例如file:Java1.png)。
您可以buttonCellProperty指定ComboBox:
comboBox.setButtonCell(new StatusListCell());
Run Code Online (Sandbox Code Playgroud)
按钮单元格用于呈现组合框“按钮”区域中显示的内容。