Im new to FXML and i'm building an application. Now i'm running into a problem that i cant fix.
I defined a combobox in the FXML and created the necesarry assocation in the controller class. But i want to add images to this combobox.
Still after hours of searching on google i'm still not able to fix this.
Can you guys please help me with a "simple" example on how to reach my goal?
Many thanks!
My current code is: (sure that there is an easier way to do this, but it works!)
ImageView img1 = new ImageView("Layout/nl.png");
ImageView img2 = new ImageView("Layout/en.png");
AnimalBoxLanguage.getItems().addAll(img1, img2);
AnimalBoxLanguage.setCellFactory(new Callback<ListView<ImageView>, ListCell<ImageView>>() {
@Override
public ListCell<ImageView> call(ListView<ImageView> p) {
return new ListCell<ImageView>() {
private final ImageView rectangle;
{
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
rectangle = new ImageView();
}
@Override
protected void updateItem(ImageView item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
rectangle.setImage(item.getImage());
setGraphic(rectangle);
}
}
};
}
});
In this article we will see how we can add icon to the item of combo box. By default there is no icon set to the icon in the combo box although we can set icon to each item with the help of setItemIcon method, below is the representation of how icon of item in combo box looks like. Attention geek!
The following is an example to add items on runtime on a JComboBox in Java: Now, click “Add” above to add a new item on runtime. After clicking, a new item would be visible in the bottom as shown in the following screenshot:
setEditable (boolean b) : the boolean b determines whether the combo box is editable or not .If true is passed then the combo box is editable or vice versa. setSelectedIndex (int i): selects the element of JComboBox at index i. showPopup () :causes the combo box to display its popup window.
JComboBox can be editable or read- only depending on the choice of the programmer . JComboBox () : creates a new empty JComboBox . JComboBox (ComboBoxModel M) : creates a new JComboBox with items from specified ComboBoxModel JComboBox (E [ ] i) : creates a new JComboBox with items from specified array.
Taking the sample erhun linked in his comment as a starting point, define the ComboBox in fxml as below so that the combo box items include Labels with graphics (these are your "icons").
<ComboBox fx:id="fruitCombo" layoutX="15.0" layoutY="33.0" prefWidth="90.0" promptText="choose">
<items>
<FXCollections fx:factory="observableArrayList">
<Label text="Apple">
<graphic>
<StackPane prefWidth="50">
<ImageView fitHeight="32" preserveRatio="true">
<image>
<Image url="http://uhallnyu.files.wordpress.com/2011/11/green-apple.jpg" preserveRatio="false" smooth="false" />
</image>
</ImageView>
</StackPane>
</graphic>
</Label>
<Label text="Pear">
<graphic>
<StackPane prefWidth="50">
<ImageView fitHeight="32" preserveRatio="true">
<image>
<Image url="http://smoothiejuicerecipes.com/pear.jpg" preserveRatio="false" smooth="false" />
</image>
</ImageView>
</StackPane>
</graphic>
</Label>
<Label text="Orange">
<graphic>
<StackPane prefWidth="50">
<ImageView fitHeight="32" preserveRatio="true">
<image>
<Image url="http://i.i.com.com/cnwk.1d/i/tim/2011/03/10/orange_iStock_000001331357X_540x405.jpg" preserveRatio="false" smooth="false" />
</image>
</ImageView>
</StackPane>
</graphic>
</Label>
</FXCollections>
</items>
</ComboBox>
And in the FruitComboController initialize method, set a custom cell for the button (the simple cell below just takes the text of the seleted item, but you could also include a graphic as well if you liked):
ListCell<Label> buttonCell = new ListCell<Label>() {
@Override protected void updateItem(Label item, boolean isEmpty) {
super.updateItem(item, isEmpty);
setText(item == null ? "" : item.getText());
}
};
fruitCombo.setButtonCell(buttonCell);
The above is just one way to do this. Alternately (and perhaps preferably) you could define a cell factory for your ComboBox
as Sebastian points out in his answer.
Output of the modified sample:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With