What is the justification for JavaFX FXML schema not supporting context menu inside the panes?

后端 未结 2 1602
半阙折子戏
半阙折子戏 2021-01-06 12:07

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

相关标签:
2条回答
  • 2021-01-06 12:40

    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?

    0 讨论(0)
  • 2021-01-06 12:45

    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).

    0 讨论(0)
提交回复
热议问题