JavaFX Utils

MultiOption

Component to help create option menus over Control objects.

If you pretend to create an option menu over a Node object you have Menu component, which creates ContextMenu on mouse left click and adds it to node.

MultiOption class is generic in T extends Control.

To help MultiOption implementation and increase legibility it was created Operations class which is class that permits to collect Operation objects. An Operation object has 2 fields :

  • private final String name : name to be displayed on menu
  • private final Runnable action : action to be executed
This 2 fields are setted on constructor and can be obtained via getter methods.

Constructors

public MultiOption(T control, Operations operations)

  • control : Control in which will be mounted option menu.
  • operations : operations to set on menu.

There are more constructors with different parameters, but all will internally call this first one.

Variations :

  • Constructors that receive Collection<Pair<String, Runnable>> actions will instantiate Operations object with it.
  • Constructors that receive IComponent<T> component obtain Control object from component.getNode().
public MultiOption(T control, Collection<Pair<String, Runnable>> actions)
public MultiOption(IComponent<T> component, Operations operations)
public MultiOption(IComponent<T> component, Collection<Pair<String, Runnable>> actions)

Usage

	Button button;

	Operations ops = new Operations();
	ops.add("A", ()->button.setGraphic(A_IMAGE));
	ops.add("B", ()->button.setGraphic(B_IMAGE));
	ops.add("C", ()->button.setGraphic(C_IMAGE));
	ops.add("D", ()->button.setGraphic(D_IMAGE));

	new MultiOption<>(button, ops).mount();
					

Links