Hello,
I am trying to add nodes to a combobox. The problem is that once I select an item in the list, then it disappears from the list. (Example of the problem included with this post)
The item is still selectable though. I have read an understood from this page where the problem lies. Basically the node is moved from the list view to the button and doesn't come back to the list view.
There is a proposed work around that uses the cellFactory.
I have tried several things, like creating events, a bit like that:
but I have the feeling that what I need is a way to create callbacks. Again I tried to create callback based on this page and the createEvent method of javaObject, but no luck...
I can't figure this one out. Does anyone know a way to implement that in B4J?
Thanks,
Jmon.
I am trying to add nodes to a combobox. The problem is that once I select an item in the list, then it disappears from the list. (Example of the problem included with this post)
The item is still selectable though. I have read an understood from this page where the problem lies. Basically the node is moved from the list view to the button and doesn't come back to the list view.
A warning about inserting Nodes into the ComboBox items list
ComboBox allows for the items list to contain elements of any type, including Node instances. Putting nodes into the items list is strongly not recommended. This is because the default cell factory simply inserts Node items directly into the cell, including in the ComboBox 'button' area too. Because the scenegraph only allows for Nodes to be in one place at a time, this means that when an item is selected it becomes removed from the ComboBox list, and becomes visible in the button area. When selection changes the previously selected item returns to the list and the new selection is removed.
There is a proposed work around that uses the cellFactory.
B4X:
The recommended approach, rather than inserting Node instances into the items list, is to put the relevant information into the ComboBox, and then provide a custom cell factory. For example, rather than use the following code:
ComboBox<Rectangle> cmb = new ComboBox<Rectangle>();
cmb.getItems().addAll(
new Rectangle(10, 10, Color.RED),
new Rectangle(10, 10, Color.GREEN),
new Rectangle(10, 10, Color.BLUE));
You should do the following:
ComboBox<Color> cmb = new ComboBox<Color>();
cmb.getItems().addAll(
Color.RED,
Color.GREEN,
Color.BLUE);
cmb.setCellFactory(new Callback<ListView<Color>, ListCell<Color>>() {
@Override public ListCell<Color> call(ListView<Color> p) {
return new ListCell<Color>() {
private final Rectangle rectangle;
{
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
rectangle = new Rectangle(10, 10);
}
@Override protected void updateItem(Color item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
rectangle.setFill(item);
setGraphic(rectangle);
}
}
};
}
});
I have tried several things, like creating events, a bit like that:
B4X:
Dim e As Object = we.CreateEvent("javafx.event.EventHandler", "wv", False)
we.RunMethod("setCellFactory", Array(e))
I can't figure this one out. Does anyone know a way to implement that in B4J?
Thanks,
Jmon.