如何在ComboBox javafx中设置ButtonCell?

时间:2017-09-07 18:27:46

标签: javafx

我正在尝试创建一个显示所选图片预览的ComboBox,但ComboBox会显示字符串值。 我读了很多建议,我发现我需要使用setButtonCell()方法,但我不知道如何。

这是我的代码:

public class ContentTabPaneController implements Initializable{

    @FXML
    private JFXComboBox<CustomComboBox> cbxDevices;

    private final ObservableList<CustomComboBox> data = FXCollections.observableArrayList();


    @Override
    public void initialize(URL location, ResourceBundle resources) {

        String smartPhoneImageSrc = "@../../image/device/iphone.png";
        String ipadImageSrc = "@../../image/device/ipad.png";

        data.clear();
        data.add(new CustomComboBox(smartPhoneImageSrc, "Smart Phone"));
        data.add(new CustomComboBox(ipadImageSrc, "Ipad"));


        cbxDevices.setCellFactory(new Callback<ListView<CustomComboBox>, ListCell<CustomComboBox>>() {
            @Override
            public ListCell<CustomComboBox> call(ListView<CustomComboBox> param) {
                ListCell<CustomComboBox> cell = new ListCell<CustomComboBox>(){
                    @Override
                    protected void updateItem(CustomComboBox item, boolean btl){
                        super.updateItem(item, btl);
                        if(item != null)
                        {
                            Image img = new Image(item.getImageSrc());
                            ImageView imgView = new ImageView(img);
                            imgView.setFitHeight(48);
                            imgView.setFitWidth(48);
                            setGraphic(imgView);
                            setText(item.getString());
                        }
                    }
                };

                return cell;
            }


        });
        cbxDevices.setItems(data);
        //cbxDevices.setButtonCell();  how can i use this methode????



    }




}

这是我的班级CustomComboBox

public class CustomComboBox {

    private String imageSrc;
    private String string;

    public CustomComboBox(String imageSrc, String string) {
        this.imageSrc = imageSrc;
        this.string = string;
    }


    public String getImageSrc() {
        return imageSrc;
    }

    public void setImageSrc(String imageSrc) {
        this.imageSrc = imageSrc;
    }

    public String getString() {
        return string;
    }

    public void setString(String string) {
        this.string = string;
    }
}

My comboBox enter image description here

1 个答案:

答案 0 :(得分:1)

只需将单元格传递给setButtonCell()

cbxDevices.setButtonCell(new ListCell<CustomComboBox>(){
    @Override
    protected void updateItem(CustomComboBox item, boolean btl){
        super.updateItem(item, btl);
        if(item != null) {
            Image img = new Image(item.getImageSrc());
            ImageView imgView = new ImageView(img);
            imgView.setFitHeight(48);
            imgView.setFitWidth(48);
            setGraphic(imgView);
            setText(item.getString());
        }
    }
});

请注意,您的单元格实现有一个错误:如果单元格被重用,以前它是非空的但现在是空的,它就不会清除文本和图形。您需要在updateItem()方法中处理所有情况(包括空项/空单元格)。另外,创建ImageView一次更好,只需在updateItem()方法中更新,而不是每次都创建一个新的。{/ p>

由于您使用相同的ListCell实现两次,因此使用命名的内部类而不是匿名类可能更好,以避免重复代码:

public class ContentTabPaneController implements Initializable{

    @FXML
    private JFXComboBox<CustomComboBox> cbxDevices;

    private final ObservableList<CustomComboBox> data = FXCollections.observableArrayList();


    @Override
    public void initialize(URL location, ResourceBundle resources) {

        String smartPhoneImageSrc = "@../../image/device/iphone.png";
        String ipadImageSrc = "@../../image/device/ipad.png";

        data.clear();
        data.add(new CustomComboBox(smartPhoneImageSrc, "Smart Phone"));
        data.add(new CustomComboBox(ipadImageSrc, "Ipad"));


        cbxDevices.setCellFactory(lv -> new CustomComboCell());
        cbxDevices.setButtonCell(new CustomComboCell());
    }

    private static class CustomComboCell extends ListCell<CustomComboBox> {

        private final ImageView imgView ;

        CustomComboCell() {
            imgView = new ImageView();
            imgView.setFitHeight(48);
            imgView.setFitWidth(48);
        }

        @Override
        protected void updateItem(CustomComboBox item, boolean btl){
            super.updateItem(item, btl);
            if(item == null) {
                setText(null);
                setGraphic(null);
            } else {
                Image img = new Image(item.getImageSrc());
                imgView.setImage(img);
                setGraphic(imgView);
                setText(item.getString());
            }
        }
    }
}