6

我有一个在 JavaFX FXML 的窗格上定义 ContextMenu 的工作示例,但不确定它是最佳的。目前,只有 JavaFX 标准控件(例如 Button、TextField)定义了用于指定弹出 ContextMenu 的属性。然而,我想让一个弹出菜单出现在窗格中的任何地方,在我的例子中是一个 VBox。

我采用扩展 VBox 的方法来支持上下文菜单。这是一个“笨拙”的解决方案,但有效。有更好的方法吗?我错过了一些基本概念吗?

这是我的解决方案...

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import custommenu.view.ContextMenuPane?>

<AnchorPane xmlns:fx="http://javafx.com/fxml" fx:controller="custommenu.controller.CustomMenuController">
    <children>
        <VBox fx:id="vbox" onContextMenuRequested="#showMenu"
            onMousePressed="#hideMenu" prefHeight="200" prefWidth="200">
        </VBox>
        <ContextMenuPane>
            <contextMenu>
                <ContextMenu fx:id="menu">
                    <items>
                        <MenuItem text="add" onAction="#add" />
                    </items>
                </ContextMenu>
            </contextMenu>
        </ContextMenuPane>
    </children>
</AnchorPane>

自定义菜单窗格...

package custommenu.view;

import javafx.scene.control.ContextMenu;
import javafx.scene.layout.Pane;

public class ContextMenuPane extends Pane {

    private ContextMenu contextMenu;

    public void setContextMenu(ContextMenu contextMenu) {
        this.contextMenu = contextMenu;
    }

    public ContextMenu getContextMenu() {
        return contextMenu;
    }

}

控制器...

package custommenu.controller;

import javafx.fxml.FXML;
import javafx.scene.control.ContextMenu;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.layout.VBox;

public class CustomMenuController {

    @FXML private VBox vbox;
    @FXML private ContextMenu menu;

    @FXML public void add() {
        System.out.println("add");
    }

    @FXML
    public void showMenu(ContextMenuEvent event) {
        System.out.println("showMenu");

        menu.show(vbox, event.getScreenX(), event.getScreenY());
        event.consume();
    }

    @FXML public void hideMenu() {
        menu.hide();
    }
}

主应用...

package custommenu;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class CustomMenuApplication extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane myPane = (Pane)FXMLLoader.load(getClass().getResource("/custommenu/custom_menu_main.fxml"));
        Scene scene = new Scene(myPane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

     public static void main(String[] args) {
         launch(args);
     }
}
4

2 回答 2

0

在此示例中,当单击左/右时,有一个具有上下文菜单的按钮,当您单击除按钮以外的其他区域时,它将打开弹出窗口,它将隐藏弹出上下文菜单

公共类主要扩展应用程序{

public static void main(String[] args) {
    launch(args);
  }
  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group(), 450, 250);
    Button notification = new Button();

    MenuItem item1 = new MenuItem("About");
    item1.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
        System.out.println("About");
      }
    });
    MenuItem item2 = new MenuItem("Preferences");
    item2.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
        System.out.println("Preferences");
      }
    });

    MenuItem item3 = new MenuItem("About");
    item1.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
        System.out.println("About");
      }
    });
    MenuItem item4 = new MenuItem("Preferences");
    item2.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
        System.out.println("Preferences");
      }
    });
    MenuItem item5 = new MenuItem("About");
    item1.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
        System.out.println("About");
      }
    });
    MenuItem item6 = new MenuItem("Preferences");
    item2.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
        System.out.println("Preferences");
      }
    });

    MenuItem item7 = new MenuItem("About");
    item1.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
        System.out.println("About");
      }
    });
    MenuItem item8 = new MenuItem("Preferences");
    item2.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
        System.out.println("Preferences");
      }
    });
    final ContextMenu contextMenu = new ContextMenu(item1, item2,item3, item4,item5, item6,item7, item8);
    contextMenu.setMaxSize(50, 50);

    contextMenu.setOnShowing(new EventHandler<WindowEvent>() {
      public void handle(WindowEvent e) {
        System.out.println("showing");
      }
    });
    contextMenu.setOnShown(new EventHandler<WindowEvent>() {
      public void handle(WindowEvent e) {
        System.out.println("shown");
      }
    });

   // contextMenu.hide();


    notification.setContextMenu(contextMenu);
    GridPane grid = new GridPane();
    grid.setVgap(4);
    grid.setHgap(10);
    grid.setPadding(new Insets(5, 5, 5, 5));
    grid.add(new Label("To: "), 0, 0);
    grid.add(notification, 1, 0);

    Group root = (Group) scene.getRoot();
    root.getChildren().add(grid);
    stage.setScene(scene);
    stage.show();
    notification.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent me)->{
        if(me.getButton()==MouseButton.PRIMARY ){
            System.out.println("Mouse Left Pressed");
            System.out.println(notification.getScaleX());
            System.out.println(notification.getScaleY());
            System.out.println(me.getScreenX());
            System.out.println(me.getScreenY());
            contextMenu.show(notification,me.getScreenX(),me.getScreenY());
        }else{
            contextMenu.hide();
        }
    });
  }

}

于 2015-03-11T09:46:42.777 回答
0

试图在弹出窗口中做到这一点?捕获 MouseEvent 检查 MouseButton.SECONDARY 并在弹出窗口中显示您的 VBox。

private void showContextMenu() // showContextMenu(MouseEvent event)
{
    Popup popup = new Popup();

    VBox content = new VBox();
    Button b = new Button("Click Me!");
    b.setOnAction(new EventHandler<ActionEvent>() { });
    content.getChildren().addAll(b);

    popup.getContent().add(content);
    popup.setX(0); // or get mouse event x and y
    popup.setY(0); // event.getY()
    popup.setAutoHide(true);
    popup.show(your popup owner);
}
于 2013-11-11T10:09:09.917 回答