小编gee*_*eef的帖子

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)。

java combobox javafx

4
推荐指数
1
解决办法
6955
查看次数

标签 统计

combobox ×1

java ×1

javafx ×1