【发布时间】:2017-09-07 18:27:45
【问题描述】:
我正在尝试创建一个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;
}
}
【问题讨论】:
标签: javafx