listjavafxarraylistcontextmenucontrolsfx

ContextMenu sub-menus from a List of Strings


I'm trying to make a ContextMenu similar to this one in the image below:

enter image description here

I want to display a list of Strings in one of the groups.

This is possible by using ActionGroup, from controlsFX library. In order to reproduce the example in the picture, use that code:

    public class HelloActionGroup{
            Label label = new Label("hello world, I'm a label");
            label.setContextMenu(ActionUtils.createContextMenu(action));
                    private Collection<? extends Action> action = Arrays.asList(
                            new ActionGroup("Group 1", new DummyAction("Action 1")),
                                                       new DummyAction("Action 2"),
                                                       new DummyAction("Action 3"))
                    );
                
                private static class DummyAction extends Action{
                        public DummyAction(String name){
                            super(name);
                        }
                    }
}

In order to make that code run in your code, simply place that label in your root Parent:

yourPaneName.getChildren().add(label);

As you can see, I have to hard code all the items being shown inside "Group 1", which are "Action1", "Action2", and "Action3". How can I place a list of Strings inside "Group 1"?

Something like:

List<String> list = new ArrayList<>();
list.add("Action1");
list.add("Action2");
list.add("Action3");
private Collection<? extends Action> action = Arrays.asList(
                                new ActionGroup("Group 1", new DummyAction(list)));

UPDATE: Managed to get really close using a loop for inserting the list's items in the ContextMenu:

List<String> list = new ArrayList<>();
        list.add("string1");
        list.add("string2");
        for (String str: list) {
            action2.add(new ActionGroup("action1",new DummyAction(str)));
        }

Result: enter image description here string2 is showed up below the selected (action1)

I know it's creating multiple ActionGroup because of the statement "new ActionGroup", now I just have to find a way to create only one ActionGroup and add multiple DummyAction(strings from list) to it


Solution

  • First, create a collection of ActionGroup, then create a separate instance from the same type of the collection (ActionGroup). After that, run a loop in order to insert every item from a list into the ActionGroup instance. Here's how it looks like:

    Collection<ActionGroup> action = new LinkedList<>();
            ActionGroup actionGroup = new ActionGroup("My list items:");
            List<String> list = new ArrayList<>();
            list.add("string 1");
            list.add("string 2");
            list.add("string 3");
            for (String str : list) {
                actionGroup.getActions().add(new DummyAction(str));
            }
            action.add(actionGroup);
            node.setContextMenu(ActionUtils.createContextMenu(action));
    

    result: enter image description here