1
我正在嘗試創建一個ComboBox
,它將顯示所選圖像的預覽,但ComboBox
會改爲顯示字符串值。 我讀了很多建議,我發現我需要使用setButtonCell()
方法,但我不知道如何。如何在ComboBox javafx中設置ButtonCell?
這是我的代碼:
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
非常感謝你,它的工作原理。 –