【问题标题】:JavaFX actions on elements of one window from another window从另一个窗口对一个窗口的元素执行 JavaFX 操作
【发布时间】:2018-11-09 21:40:31
【问题描述】:

请帮我解决问题。有两个 fxml 文件及其控制器:

sample.fxml, its controller ControllerMain (main window of the program)
find_win.fxml, its ControllerFind controller (modal window "text search")

在模态窗口find_win.fxml中有一个TextField用于输入搜索文本,而Find按钮,当被点击时,ControllerFind必须处理点击并调用搜索方法并在TextArea元素中高亮搜索文本sample.fxml 窗口。

<fx: include source = "sample.fxml" fx: id = "textAreaOne" />

并且ControllerFind控制器对ControllerMain的继承无助于实现解决方案-在第一种情况下,整个窗口标记完全包含在模态窗口中,在第二种情况下,java.lang.reflect.InvocationTargetException在对 TextArea 进行操作期间返回。

如何从另一个窗口对一个窗口的元素执行动作?

【问题讨论】:

    标签: java javafx


    【解决方案1】:

    我在其他地方找到了解决方案。感谢 Antizam 同志,他试图提供帮助,但不太明白我需要什么。

    here is描述的解决方案。

    简而言之,需要创建一个controller-parent 的实例和一个将controller-parent 的实例作为controller-child 中的参数的方法。当从父控制器打开新窗口时,获取子控制器的实例并通过创建的方法“this”指示它。

    此外,在子控制器中,可以访问父控制器的元素。

    控制器父级:

    package sample;
    
    public class ControllerMain {
    
        private ControllerFind children;  // controller-child
    
        //main-window
        @FXML
        public TextArea textAreaOne;
        @FXML
        public MenuItem findMenuItem;
    
        public void findAction(ActionEvent actionEvent) {
            try {
                Stage stageFind = new Stage();
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(getClass().getResource("FXML/find_win.fxml"));
                Parent root = loader.load();
                stageFind.setTitle("Find");
                stageFind.setMinHeight(200);
                stageFind.setMinWidth(150);
                stageFind.setResizable(false);
                stageFind.setScene(new Scene(root));
                stageFind.getIcons().add(new Image("image/search.png"));
                stageFind.initModality(Modality.NONE);
                stageFind.show();
    
                children = loader.getController(); //getting controller of window find_win.fxml
                children.setParent(this);   //setting parent of the controller-child - this
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    控制器子:

    package sample.Controllers;
    
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.scene.Node;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextArea;
    import javafx.scene.control.TextField;
    import javafx.stage.Stage;
    import sample.Classes.DialogManager;
    import sample.ControllerMain;
    
    public class ControllerFind {
    
        //Window "Find"
        @FXML public TextField searchTextField;
        @FXML public Label findTextLabel;
        @FXML public Button okTextFindButton;
        @FXML public Button cancelTextFindButton;
        private String text;
        private ControllerMain controller;
    
        public void setParent (ControllerMain controller){
            this.controller = controller;
        }
    
        public ControllerFind getThis(){
            return this;
        }
    
        public void initialize(){
            System.out.println("psvm");
        }
    
        public void textFindOkButtonAction(ActionEvent actionEvent) {
            text = (searchTextField.getText());
            if (text.equals("")) {
                DialogManager.showInfoDialog("Error!", "Enter text what you are looking for!");
            } else {
                if (controller.textAreaOne.getText() != null && !controller.textAreaOne.getText().isEmpty()) {
                    int index = controller.textAreaOne.getText().indexOf(text);
                    if (index == -1) {
                        DialogManager.showInfoDialog("Result", "There isn't text what you are looking for");
                    } else {
                        controller.textAreaOne.selectRange(index, index + text.length());
                    }
                } else {
                    DialogManager.showInfoDialog("Error", "TextArea is empty!");
                }
            }
        }
    
        public void textFindCancelButtonAction(ActionEvent actionEvent) {
            Node source = (Node) actionEvent.getSource();
            Stage stage = (Stage) source.getScene().getWindow();
            stage.close();
        }
    }
    

    【讨论】:

      【解决方案2】:

      在这种情况下,对于 Windows 通信来说,最好使用 TextInputDialog 这样的东西:

      @Override
          public void start(Stage primaryStage){
      
              Button btn=new Button("Click");
              VBox vbox=new VBox();
              vbox.setAlignment(Pos.CENTER);
              vbox.getChildren().addAll(btn);
              Scene scene=new Scene(vbox, 200,200);
      
              btn.setOnAction(e->
              {
                  TextInputDialog dialog=new TextInputDialog();
                  Optional<String> result = dialog.showAndWait();
                  if (result.isPresent()){
                      System.out.println(result.get());
                  }
              });
              primaryStage.setScene(scene);
              primaryStage.show();
          }
      

      但是文本突出显示并不是一件容易的事。 您可以对 TextArea 文本进行字符串搜索,将其与另一个窗口的结果进行比较,然后通过

      突出显示它
      textArea.getSelectedRange(firstIndex, lastIndex);
      

      firstIndex 和 lastIndex 是我们正在搜索的单词的 textArea 文本的索引。然后在每次单击时都有按钮显示文本中出现的下一个单词并突出显示它。但是,如果您坚持同时突出显示单词的每个实例,我建议您使用RichTextFX。

      【讨论】:

        猜你喜欢
        • 2015-08-15
        • 2014-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-11
        • 1970-01-01
        • 2021-05-09
        • 2019-01-06
        相关资源
        最近更新 更多