【问题标题】:How to send event from child class to parent class如何将事件从子类发送到父类
【发布时间】:2020-03-15 02:55:23
【问题描述】:

我想知道如何将事件从孩子发送给父母。 我写了这段代码,如果选择目录,我可以在 ImageView 上看到目录中的第一张图片。

按下按钮并选择目录后,我想将路径发送到 ParentController。 但是,现在我无法发送,因为在创建窗口时调用了 getCurrentPath()。

父控制器

@FXML private Button openDirButton;
@FXML private ImageView mainImageView;

@FXML
public void initialize(URL location, ResourceBundle resources) {

    // Choosing Directory Button
    OpenDirectoryButton ODB = new OpenDirectoryButton();
    ODB.getDirSetImageSetListView(openDirButton, mainImageView);
    String currentPath = ODB.getCurrentPath();
    System.out.println(currentPath); // null

ChildController

public class OpenDirectoryButton {

    public static String path;
    public Button getDirSetImageSetListView(Button button, ImageView imageView) {
        button.setOnAction(actionEvent -> {
            // This is a class I made
            DirectoryChoose DC = new DirectoryChoose();
            // Get a Directory Path
            path = DC.getPath();
            // Get a list of path of images
            imageList = DC.getImageList(path);
            image = new Image("file:///" + path + File.separator + imageList.get(0));
            imageView.setImage(image);
        });
        return button;
    }
    public String getCurrentPath() {
        return path;
    }
}

【问题讨论】:

  • 从理论上讲,您可以通过将侦听器传递给得到通知的方法来执行类似的操作(或者简单地使用侦听器来传递为此目的传递的ImageViewimage 属性)。但是,您的类之间的职责分配不是最理想的,并且命名也不完美; OpenDirectoryButton,与顾名思义相反,不是按钮,而是能够创建按钮的类。在这种情况下,将static 用于OpenDirectoryButton.path 几乎可以肯定是错误的...

标签: java javafx fxml


【解决方案1】:

与事件一起使用有几种方法。

1)https://github.com/AlmasB/FXEventBus。您可以将其集成到您的项目中,然后可以用来操作事件。

2)您可以将您的类声明为静态字段并发送给您的孩子,然后从子类中您将使用您的字段 不是很好的示例。

父控制器

在类的字段中

public static ParentConrtroller instance; // Not good declare field in public

    ***
    public void inititalize(URL location, ResourceBundle resources){
     instance = this;
}

儿童控制器

ParentController.instance //and every public method of Parent is available for your 

【讨论】:

    【解决方案2】:

    将消费者传递给getDirSetIgmageSetListView

    父控制器

    ODB.getDirSetImageSetListView(openDirButton, mainImageView, path->{
        System.out.println(path);
    });
    

    儿童控制器

    public Button getDirSetImageSetListView(Button button, ImageView imageView, Consumer<String> pathConsumer) {
        button.setOnAction(actionEvent -> {
            // This is a class I made
            DirectoryChoose DC = new DirectoryChoose();
            // Get a Directory Path
            path = DC.getPath();
            // Get a list of path of images
            imageList = DC.getImageList(path);
            image = new Image("file:///" + path + File.separator + imageList.get(0));
            imageView.setImage(image);
            imageConsumer.accept(path); //execute consumer with your path variable
        });
        return button;
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 1970-01-01
      • 2012-10-31
      • 1970-01-01
      • 2022-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多