4

It is possible to add a context menu to a scroll pane, but not to other types of panes. Why?

ajeh
  • 2,652
  • 2
  • 34
  • 65

2 Answers2

5

How FXML Works

FXML works by introspecting on the Java API using reflection (or by using specialized builder classes). More information on FXML works can be found in the Introduction to FXML documentation.

Why ContextMenus can't be defined on Panes in JavaFX using FXML Markup

Control has a contextMenu property. A ScrollPane is a Control. Other pane types such as StackPane are not controls. As there is no corresponding property in these other pane types which could be set to contain a reference to a contextMenu, you can't define a contextMenu on these pane types using FXML.

For similar reasons, you can't define a Tooltip on a Pane either.

How to define a ContextMenu for a Panes in an FXML Controller

You can still set a context menu on panes (and any other arbitrary nodes which are not controls) via code, using the contextMenu show API, for example by placing the following code in your FXML controller.

@FXML StackPane stack;

// . . .

public void initialize() {
    final ContextMenu contextMenu = new ContextMenu(new MenuItem("xyzzy"));
    stack.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent mouseEvent) {
            contextMenu.show(
                    stack,
                    mouseEvent.getScreenX(), 
                    mouseEvent.getScreenY()
            );
        }
    });
}

Why not add a ContextMenu property

Node could have a contextMenu property, which would allow ContextMenus to be defined on Panes via FXML Markup.

The reason why Node does not have a contextMenu property but Control does is because ContextMenu is itself a Control. Adding a ContextMenu property to node would mean that the core scene graph model code for the JavaFX implementation would have a dependency on the controls module (which would have a dependency on the scene graph module), hence a circular dependency. This would prevent the shipping of a very light Java runtime system which included the core JavaFX scene graph and rendering engine but did not include controls (not that anybody ships such a system today).

How to File Feature Requests

If you think the system should be changed to allow definition of context menus on arbitrary panes using SceneBuilder, then you can file a feature request against the JavaFX issue tracker (if you do so, include a link back to this question in the feature request).

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Well, correct me if I am wrong, but if the panes accept other types of controls, I do not see why context menus should be treated any different. Moreso, each cell of a grid panel and similar panels that have cells, should accept their own, separate popup menus too. – ajeh Oct 23 '13 at 13:46
  • 1
    I don't think you understood my answer at all and unfortunately I don't know how to explain it more clearly. – jewelsea Oct 23 '13 at 16:51
  • 1
    I understood every letter of it and do not need any further explanations. We are simply looking at the issue from different angles. You from strictly technical POV of a library developer, and I am from an application developer perspective. Technically, there is a challenge for the library developers in adding a popup menu to the panes. Why it has to stay that way is a different topic. – ajeh Oct 23 '13 at 17:04
1

Described method to open popup leads to multiple popups open if every node in the scene graph want to open context menu. Consuming of event is definitely needed.

See also discussion at Using FXML to Create ContextMenu within a Pane It provides working answer to this problem.

BTW, Node.onContextMenuRequested(...) should be used instead, yes?

Community
  • 1
  • 1
  • The amount of boilerplate code required for this rudimentary simplistic task is horrible. – ajeh Mar 09 '16 at 16:28