Skip to content

Latest commit

 

History

History
66 lines (49 loc) · 3.21 KB

2-for.md

File metadata and controls

66 lines (49 loc) · 3.21 KB

For-Loops Javadocs

For-Loops can be used to easily display a node/subcomponent for all items in a list. Whenever an item is added to or removed from the list, the list of nodes updates accordingly.

In order to create a for loop, acquire an instance of the FxFor class (e.g. using Dagger) and call the of method. The easiest form of a For-Loop can be achieved like this:

fxFor.of(container, items, myComponentProvider);

This will create a component for each item in the list items and add it to the children of the container (e.g. a VBox). If the component implements the ReusableItemComponent interface, setItem will be called instead of creating a new instance. For more information see this section.

Currently, no information is passed to the created component. In order to pass static information you can add parameters like you would when using the show-method using a map.

When a new component is created, the parameters item and list are automatically added to the map. The item parameter contains the current item of the component and the list parameter contains the list of all items. If parameters with the same key are already present in the map, they will not be overwritten.

fxFor.of(container, items, myComponentProvider, Map.of("key", value));
fxFor.of(container, items, myComponentProvider, params); // Parameters can be taken from the @Params annotation for example

If you want to pass dynamic information like binding the item to its component, you can use an BiConsumer. The BiConsumer allows to define actions for initializing each component based on its item.

fxFor.of(container, items, myComponentProvider, (component, item) -> {
    component.setItem(item);
    component.foo();
    component.bar();
});

fxFor.of(container, items, myComponentProvider, ExampleComponent::setItem); // Short form using method references
fxFor.of(container, items, myComponentProvider, Map.of("key", value), ExampleComponent::setItem); // Static and dynamic information can be passed together

Instead of a component you can also define a basic JavaFX node to display for every item.

fxFor.of(container, items, () -> new Button("This is a button!"));
fxFor.of(container, items, () -> new VBox(new Button("This is a button!"))); // Nodes can have children

Unlike with components, it is not possible to pass static information in the form of parameters to nodes, as there is no way of accessing them in the code. However, dynamic information in the form of an BiConsumer can be used just like with components.

fxFor.of(container, items, () -> new Button(), (button, item) -> {
    button.setText("Delete " + item.name());
    button.setOnAction(event -> items.remove(item));
});

In order to destroy components generated by the For-loops, you can use the dispose() method of the For class or add the return value of the disposable() method to your list of disposables.


⬅ Subscriber | Overview | History ➡