JavaFX ChoiceBox ContextMenu位置

时间:2014-04-03 10:15:27

标签: java css javafx contextmenu

我是stackoverflow和Java编程的新手,虽然我确实有一些小的编程背景。

我正在使用JavaFX 8.0.0-b132和JavaSE 8构建一个小程序......

程序有一个GUI,这个GUI有一个ChoiceBox。布局的架构是: AnchorPane - VBox - TitledPane - GridPane - HBox - ChoiceBox

我已经使用CSS广泛修改了ChoiceBox。

除了一个问题外,一切都正常:

当我第一次启动程序并打开ChoiceBox(每次单击鼠标或使用show())时,它将在底部边缘显示它的ContextMenu;然而,当我关闭并再次打开它(用鼠标或编程)时,它将在ChoiceBox打开按钮(错误的y位置)的顶部显示ContextMenu,这样当前所选单元格的底部与打开的底部重合 - 按钮(意思是在我选择另一个单元格后再次打开它会改变位置)。

如何防止这种情况并使ContextMenu始终显示在底部,就像它最初一样? 以下是受影响控件的程序GUI部分片段:

编辑:问题解决了:显然这种行为是预期的ChoiceBox行为;我在JavaFX论坛上发帖表示,最好从一开始就让弹出窗口表现得更好,即第一次打开弹出窗口以免出现错误。

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.input.MouseEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.geometry.Insets;
import javafx.collections.*;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Side;

public class bugdemo extends Application {

    // Elements needed in both threads

    RadioButton radioBoxersOne = new RadioButton("One");
    RadioButton radioBoxersTwo = new RadioButton("Two");

    TextField warmupText = new TextField("skip warm up");
    TextField noCyclesText = new TextField("3");
    TextField cycleTimeText = new TextField("45");
    TextField restingTimeText = new TextField("30");

    String[] warmupTFtext = {"skip warm up", "\u00FCberspringen"};

    public static void main(String... args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        // root container
        AnchorPane anchor = new AnchorPane();

        // Options TitledPane
        GridPane trainingOptions = new GridPane();
        ToggleGroup toggleGroup = new ToggleGroup();
        radioBoxersOne.setToggleGroup(toggleGroup);
        radioBoxersTwo.setToggleGroup(toggleGroup);
        radioBoxersOne.setSelected(true);
        HBox hbToggle = new HBox(15, radioBoxersOne, radioBoxersTwo);
        hbToggle.setPadding(new Insets(5,0,0,0));

        ObservableList<String> langList = FXCollections.observableArrayList("English", "Deutsch");
        ChoiceBox<String> langBox = new ChoiceBox<String>(langList);
        langBox.setValue("English");
        HBox hbLangBox = new HBox(0, langBox);
        hbLangBox.setPadding(new Insets(5,0,0,0));

        Label warmupOptionsLabel = new Label("Warm up time (min): ");
        Label noCyclesOptionsLabel = new Label("Number of cycles: ");
        Label cycleOptionsLabel = new Label("Cycle time (sec): ");
        Label restingOptionsLabel = new Label("Resting time (sec): ");
        Label noBoxersOptionsLabel = new Label("Number of boxers: ");
        Label langOptionsLabel = new Label("Language: ");

        trainingOptions.add(warmupOptionsLabel, 0, 0);
        trainingOptions.add(noCyclesOptionsLabel, 0, 1);
        trainingOptions.add(cycleOptionsLabel, 0, 2);
        trainingOptions.add(restingOptionsLabel, 0, 3);
        trainingOptions.add(noBoxersOptionsLabel, 0, 4);
        trainingOptions.add(langOptionsLabel, 0, 5);
        trainingOptions.add(warmupText, 1, 0);
        trainingOptions.add(noCyclesText, 1, 1);
        trainingOptions.add(cycleTimeText, 1, 2);
        trainingOptions.add(restingTimeText, 1, 3);
        trainingOptions.add(hbToggle, 1, 4);
        trainingOptions.add(hbLangBox, 1, 5);

        TitledPane TPane_in = new TitledPane("Options", trainingOptions);

        // Options TPane and Buttons together as UI (input)
        VBox UI = new VBox(10, /* hbButtons ,*/ TPane_in);
        anchor.getChildren().add(UI);
        AnchorPane.setLeftAnchor(UI, 25.0);
        AnchorPane.setTopAnchor(UI, 100.0);

        // EventHandlers for the radio-buttons
        radioBoxersOne.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent e) {
/*              if (!singleBoxer){
                    trainingProgress.getChildren().remove(boxerLabel);
                    trainingProgress.getChildren().remove(boxerText);
                }
                changeTextFlow();
                singleBoxer = true; */
            }
        });

        radioBoxersTwo.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent e) {
/*              singleBoxer = false;
                trainingProgress.add(boxerLabel, 0, 3);
                trainingProgress.add(boxerText, 1, 3);
                changeTextFlow(); */
            }
        });

        // "EventHandler" for the language ChoiceBox
        langBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
            public void changed(ObservableValue ov, Number value, Number new_value) {
/*              language = (int)new_value;
                changeLanguage(); */
            }
        });

        // initialize scene and show stage
        Scene scene = new Scene(anchor, 800, 650);
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);       // resizeable on/off
        // import CSS file
        // scene.getStylesheets().add(Letsbox.class.getResource("Training.css").toExternalForm());
        primaryStage.show();
        langBox.show();
        langBox.hide();
        langBox.show();
    }
}

在此示例中,不使用CSS,但问题仍然存在......

我尝试用以下方法修复它:

/*      langBox.setOnMousePressed(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent e) {
                ContextMenu cm = langBox.getContextMenu();
                cm.show(cm.getOwnerNode(), Side.BOTTOM, 0.0, 0.0);
            }
        });  */

但它会抛出NullPointerException ...

请告诉我如何解决这个问题:)

1 个答案:

答案 0 :(得分:-1)

public class Main  extends Application{

    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();
        notification.setPrefSize(80, 60);
        notification.setAlignment(Pos.CENTER);
        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,Side.TOP,0,0);
                //contextMenu.show(notification,me.getScreenX(),me.getScreenY());
            }else{
                contextMenu.hide();
            }
        });
      }

}