【问题标题】:using modality in a popup window javafx在弹出窗口 javafx 中使用模态
【发布时间】:2014-01-07 11:49:19
【问题描述】:

我在窗格中保留了一个弹出对话框,它位于其他组件的顶部。现在我想禁用访问程序的所有其他组件。怎么做?

【问题讨论】:

  • 我也有同样的问题...试试这个stackoverflow.com/questions/19953306/…
  • 这是个好问题。我知道所有给出的答案。但这似乎是popup类的一个重大错误。如果您无法设置其模式,弹出窗口有什么用?我不想为简单的模态输入创建一个新的“舞台”。我爱java。但这很奇怪......

标签: javafx modality


【解决方案1】:

popup API 没有您想要的 initModality(Modality.APPLICATION_MODAL); 方法。在这种情况下,您可以将您的弹出窗口设置为舞台,并使用上述方法。

【讨论】:

    【解决方案2】:

    这是解决方案中的@Xsleek,示例代码:-

    package popupexample;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBoxBuilder;
    import javafx.scene.text.Text;
    import javafx.stage.Modality;
    import javafx.stage.Stage;
    
    /**
     *
     * @author reegan
     */
    public class PopUpExample extends Application {
    
        @Override
        public void start(Stage primaryStage) {
            Button btn = new Button();
            btn.setText("Say 'Hello World'");
            btn.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    popupErrorMsg();
                }
            });
    
            StackPane root = new StackPane();
            root.getChildren().add(btn);
    
            Scene scene = new Scene(root, 300, 250);
    
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        /**
         * The main() method is ignored in correctly deployed JavaFX application.
         * main() serves only as fallback in case the application can not be
         * launched through deployment artifacts, e.g., in IDEs with limited FX
         * support. NetBeans ignores main().
         *
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
    
    
        public void popupErrorMsg() {
            final Stage myDialog = new Stage();
            myDialog.initModality(Modality.APPLICATION_MODAL);
            Button okButton = new Button("Ok");
            okButton.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent arg0) {
                    myDialog.close();
                }
            });
            Date todayDate = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            Scene myDialogScene = new Scene(VBoxBuilder.create()
                    .children(new Text("Please Enter Validate Date \n \t "+ dateFormat.format(todayDate)), okButton)
                    .spacing(30)
                    .alignment(Pos.CENTER)
                    .padding(new Insets(10))
                    .build());
    
            myDialog.setScene(myDialogScene);
            myDialog.show();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多