Java : 组合框不显示所选图像
Java : Combobox Doesn't Display Selected Image
我正在尝试显示二维组合框阵列 (10 x 10)
并让它代表一个董事会。
在每个组合框中,我想 select 一张图片。
这可行,但唯一的问题是它不显示 selected 图像。
仅对于 10 个组合框中的 5 个,它显示 Select 图像。
这几天一直困扰着我,我找不到解决办法。
这就是代码的样子。
附加信息:(它在一个网格窗格中,底部的图片显示了它的外观)
for(int i = 0;i<(comboboxarray.length)*8;i=i+8)
{
for(int j = 0;j<comboboxarray[i/8].length;j++)
{
ComboBox box = new ComboBox(objectenlist);
box.setItems(objectenlist);
box.setPrefSize(50, 50);
box.getSelectionModel().select(1);
box.setCellFactory(
new Callback<ListView<ImageView>, ListCell<ImageView>>()
{
@Override public ListCell<ImageView> call(ListView<ImageView> param)
{
final ListCell<ImageView> cell = new ListCell<ImageView>()
{
{
super.setPrefWidth(USE_PREF_SIZE);
}
@Override public void updateItem(ImageView item,
boolean empty)
{
super.updateItem(item, empty);
if (item != null)
{
switch (item.getId()) {
case "MUUR":
setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/gui/images/Muur.png"))));
break;
case "EMPTY":
setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/gui/images/empty.png"))));
break;
case "DOEL":
setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/gui/images/VELDDOEL.png"))));
break;
case "VELD":
setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/gui/images/VELD.png"))));
break;
case "KIST":
setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/gui/images/KIST.png"))));
break;
case "START":
setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/gui/images/START.png"))));
break;
}
}else{
setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/gui/images/empty.png"))));
}
}
};
return cell;
}
});
comboboxarray[i/8][j] = box;
add(comboboxarray[i/8][j],i , j, 8, 1);
}
}
![示例][1]
请阅读 ComboBox
的 Java 文档:
It is important to note that if a cell factory is set on a ComboBox,
cells will only be used in the ListView that shows when the ComboBox
is clicked. If you also want to customize the rendering of the
'button' area of the ComboBox, you can set a custom ListCell instance
in the button cell property.
总之,你还需要设置combobox.setButtonCell()
。但是,更好的选择是在您的情况下使用 ChoiceBox
。
感谢您的宝贵建议。
添加此代码为我解决了这个问题!
box.setButtonCell(new ListCell<ImageView>()
{
@Override protected void updateItem(ImageView item, boolean empty)
{
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
setGraphic(new ImageView(item.getImage()));
}
}
});
我正在尝试显示二维组合框阵列 (10 x 10) 并让它代表一个董事会。 在每个组合框中,我想 select 一张图片。 这可行,但唯一的问题是它不显示 selected 图像。 仅对于 10 个组合框中的 5 个,它显示 Select 图像。 这几天一直困扰着我,我找不到解决办法。 这就是代码的样子。 附加信息:(它在一个网格窗格中,底部的图片显示了它的外观)
for(int i = 0;i<(comboboxarray.length)*8;i=i+8)
{
for(int j = 0;j<comboboxarray[i/8].length;j++)
{
ComboBox box = new ComboBox(objectenlist);
box.setItems(objectenlist);
box.setPrefSize(50, 50);
box.getSelectionModel().select(1);
box.setCellFactory(
new Callback<ListView<ImageView>, ListCell<ImageView>>()
{
@Override public ListCell<ImageView> call(ListView<ImageView> param)
{
final ListCell<ImageView> cell = new ListCell<ImageView>()
{
{
super.setPrefWidth(USE_PREF_SIZE);
}
@Override public void updateItem(ImageView item,
boolean empty)
{
super.updateItem(item, empty);
if (item != null)
{
switch (item.getId()) {
case "MUUR":
setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/gui/images/Muur.png"))));
break;
case "EMPTY":
setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/gui/images/empty.png"))));
break;
case "DOEL":
setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/gui/images/VELDDOEL.png"))));
break;
case "VELD":
setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/gui/images/VELD.png"))));
break;
case "KIST":
setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/gui/images/KIST.png"))));
break;
case "START":
setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/gui/images/START.png"))));
break;
}
}else{
setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/gui/images/empty.png"))));
}
}
};
return cell;
}
});
comboboxarray[i/8][j] = box;
add(comboboxarray[i/8][j],i , j, 8, 1);
}
}
![示例][1]
请阅读 ComboBox
的 Java 文档:
It is important to note that if a cell factory is set on a ComboBox, cells will only be used in the ListView that shows when the ComboBox is clicked. If you also want to customize the rendering of the 'button' area of the ComboBox, you can set a custom ListCell instance in the button cell property.
总之,你还需要设置combobox.setButtonCell()
。但是,更好的选择是在您的情况下使用 ChoiceBox
。
感谢您的宝贵建议。 添加此代码为我解决了这个问题!
box.setButtonCell(new ListCell<ImageView>()
{
@Override protected void updateItem(ImageView item, boolean empty)
{
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
setGraphic(new ImageView(item.getImage()));
}
}
});