JavaFX 组合框图像 [英] JavaFX ComboBox Image

查看:31
本文介绍了JavaFX 组合框图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 ComboBox 来显示所选 Image 的预览,但 ComboBox 反而显示字符串值.

似乎唯一有效的方法是创建 NodeComboBox,但这会导致一旦选择的选项从下拉菜单中消失,如果有人有任何建议,我们将不胜感激.

我的代码如下:

String notOnLine = "file:Java1.png";String onLine = "file:Java2.png";ObservableList选项 = FXCollections.observableArrayList();options.addAll(notOnLine, onLine);最终的 ComboBox组合框 = 新组合框(选项);comboBox.setCellFactory(c -> new StatusListCell());

ListCell:

 public class StatusListCell extends ListCell{protected void updateItem(String item, boolean empty){super.updateItem(item, 空);设置图形(空);设置文本(空);如果(项目!=空){ImageView imageView = new ImageView(new Image(item));imageView.setFitWidth(40);imageView.setFitHeight(40);设置图形(图像视图);setText("a");}}}

我希望在列表关闭后将图像显示在 ComboBox 本身中.现在它只是显示 URL(例如 file:Java1.png).

解决方案

您可以指定 buttonCellProperty:

comboBox.setButtonCell(new StatusListCell());

<块引用>

按钮单元用于渲染 ComboBox 中显示的内容按钮"区域.

I am trying to create a ComboBox that will display a preview of selected Image, but the ComboBox displays the string value instead.

The only way appears to work is to create ComboBox of Node, but that causes once selected option disappear from the drop down menu, would appreciate if someone has any suggestions.

My code below:

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());

and the 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");
        }
    }

}

I'd like the image to be displayed in the ComboBox itself once the list is closed. Right now it's just showing the URL (e.g. file:Java1.png).

解决方案

You can specify the buttonCellProperty of the ComboBox:

comboBox.setButtonCell(new StatusListCell());

The button cell is used to render what is shown in the ComboBox 'button' area.

这篇关于JavaFX 组合框图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆