0

多くの BorderPane コンポーネントのステージとして使用される FlowPane があります。BorderPane コンポーネントのコンテキスト メニューを作成しました。FlowPane にも 2 つ目のコンテキスト メニューを追加したいのですが、BorderPane をクリックすると 2 つのコンテキスト メニューが表示されるようになりました。BorderPane から 2 番目のコンテキスト メニューを除外する方法が必要です。

FlowPane flow;

    public ScrollPane infrastructurePane()
    {

        flow = new FlowPane();
        flow.setPadding(new Insets(5, 5, 5, 5));
        flow.setVgap(15);
        flow.setHgap(15);
        flow.setAlignment(Pos.CENTER);

        ScrollPane scroll = new ScrollPane();
        scroll.setStyle("-fx-background-color:transparent;");

        Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();

        scroll.setPrefSize(primaryScreenBounds.getWidth(), primaryScreenBounds.getHeight());
        scroll.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);    // Horizontal scroll bar
        scroll.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);    // Vertical scroll bar
        scroll.setContent(flow);
        scroll.viewportBoundsProperty().addListener(new ChangeListener<Bounds>()
        {
            @Override
            public void changed(ObservableValue<? extends Bounds> ov, Bounds oldBounds, Bounds bounds)
            {
                flow.setPrefWidth(bounds.getWidth());
                flow.setPrefHeight(bounds.getHeight());
            }
        });

        //flow.setPrefWrapLength(170); // preferred width allows for two columns
        flow.setStyle("-fx-background-color: white;");

        for (int i = 0; i < 28; i++)
        {
            flow.getChildren().add(generatePanel(flow));

        }
        scroll = makeTabContextMenu(scroll);

        String cssURL = "/com/dx57dc/css/ButtonsDemo.css";
        String css = this.getClass().getResource(cssURL).toExternalForm();
        flow.getStylesheets().add(css);

        return scroll;

    }

    public BorderPane generatePanel(FlowPane flow)
    {
        HBox thb = new HBox();
        thb.setPadding(new Insets(10, 10, 10, 10));
        thb.setStyle("-fx-background-color: #006699;");

        HBox bhb = new HBox();
        bhb.setPadding(new Insets(10, 10, 10, 10));
        bhb.setStyle("-fx-background-color: #B0B0B0;");

        DropShadow ds = new DropShadow();
        ds.setOffsetY(3.0);
        ds.setOffsetX(3.0);
        ds.setColor(Color.GRAY);

        BorderPane bp = new BorderPane();
        bp.setEffect(ds);
        bp.setCache(true);
        bp.setPrefSize(320, 180);
        bp.setMaxSize(320, 180);
        bp.setId("app");

        bp.setStyle("-fx-background-color: linear-gradient(to bottom, #f2f2f2, #d4d4d4);"
                + " -fx-border: 2px solid; -fx-border-color: white;");

        bp.setTop(thb);
        bp.setBottom(bhb);

        ScrollPane sp = new ScrollPane();

        bp = panelContextMenu(bp);
        bp = mouseOver(bp);

        return bp;
    }

    public BorderPane mouseOver(final BorderPane bp)
    {

        bp.setOnMouseEntered(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent t)
            {

                bp.setStyle("-fx-border: 2px solid; -fx-border-color: black;");

            }
        });

        bp.setOnMouseExited(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent t)
            {
                bp.setStyle("-fx-border: 2px solid; -fx-border-color: white;");

            }
        });

        bp.setOnMouseClicked(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent me)
            {

                if (me.getButton().equals(MouseButton.PRIMARY) && me.getClickCount() % 1 == 0)
                {
                    bp.setPrefSize(480, 280);
                    bp.setMaxSize(480, 280);

                }

                if (me.getButton().equals(MouseButton.PRIMARY) && me.getClickCount() % 2 == 0)
                {
                    bp.setPrefSize(320, 180);
                    bp.setMaxSize(320, 180);
                }
            }
        });

        return bp;
    }

    private ScrollPane makeTabContextMenu(ScrollPane scroll)
    {

        ContextMenu contextMenu = new ContextMenu();

        MenuItem item1 = new MenuItem("New");
        item1.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                System.out.println("New");
                flow.getChildren().add(generatePanel(flow));
            }
        });

        // Rename
        MenuItem rename = new MenuItem("Rename");
        final TextField textField = new TextField();

        rename.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                System.out.println("Rename");
                //textField.setText(label.getText());
                //tab.setGraphic(textField);
                textField.selectAll();
                textField.requestFocus();
            }
        });

        textField.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent event)
            {
                //label.setText(textField.getText());
                //tab.setGraphic(label);
            }
        });

        textField.focusedProperty().addListener(new ChangeListener<Boolean>()
        {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable,
                    Boolean oldValue, Boolean newValue)
            {
                if (!newValue)
                {
                    //label.setText(textField.getText());
                    //tab.setGraphic(label);
                }
            }
        });

        // Orientation
        Menu tabOrientation = new Menu("Orientation");

        // Orientation sub menu - Top, Left, Right, Bottom
        MenuItem tabOrientationSubTop = new MenuItem("Top");
        tabOrientationSubTop.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                //tabPane.setSide(Side.TOP);
            }
        });
        MenuItem tabOrientationSubLeft = new MenuItem("Left");
        tabOrientationSubLeft.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                //tabPane.setSide(Side.LEFT);
            }
        });
        MenuItem tabOrientationSubRight = new MenuItem("Right");
        tabOrientationSubRight.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                //tabPane.setSide(Side.RIGHT);
            }
        });
        MenuItem tabOrientationSubBottom = new MenuItem("Bottom");
        tabOrientationSubBottom.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                //tabPane.setSide(Side.BOTTOM);
            }
        });

        tabOrientation.getItems().addAll(tabOrientationSubTop, tabOrientationSubLeft, tabOrientationSubRight, tabOrientationSubBottom);

        MenuItem item3 = new MenuItem("Close Tab");
        item3.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                System.out.println("Close Tab");
                // Close the Tab and remove it from the TabPane
                //tabPane.getTabs().remove(tab);
                // If there are no tabs into the TabPane fill all empty space by resizing the near components
//                if (tabPane.getTabs().size() == 0)
//                {
//                    ActionTabs.getMainPane().setManaged(false); // Exclude the panel to fill the empty space
//                }
            }
        });

        MenuItem item4 = new MenuItem("Close All Panels");
        item4.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                System.out.println("Close All Panels");
                flow.getChildren().clear();
            }
        });

        contextMenu.getItems().addAll(item1, rename, tabOrientation, item3, item4);

        scroll.setContextMenu(contextMenu);

        return scroll;
    }

    public BorderPane panelContextMenu(final BorderPane bp)
    {

        final ContextMenu contextMenu = new ContextMenu();

        MenuItem item1 = new MenuItem("About");
        item1.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                System.out.println("About");
            }
        });
        MenuItem item2 = new MenuItem("Preferences");
        item2.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                System.out.println("Preferences");
            }
        });
        MenuItem item3 = new MenuItem("Close");
        item3.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                flow.getChildren().remove(bp);
            }
        });
        contextMenu.getItems().addAll(item1, item2, item3);

        bp.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>()
        {
            @Override
            public void handle(ContextMenuEvent event)
            {
                contextMenu.show(bp, event.getScreenX(), event.getScreenY());
            }
        });

        bp.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent event)
            {
                contextMenu.hide();
            }
        });

        return bp;
    }

FlowPane 本体に対してのみ makeTabContextMenu() を表示する方法はありますか?

4

1 に答える 1

1

イベントを消費して設定しようとしましたか?

  @Override
  public void handle(MouseEvent t)
  {
    ... 
    t.consume()
    ...
  }
于 2013-08-05T06:09:39.767 に答える