如何从子类发送事件到父类。

huangapple go评论93阅读模式
英文:

How to send event from child class to parent class

问题

ParentController

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

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

    // 选择目录按钮
    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 -> {
            // 这是我创建的一个类
            DirectoryChoose DC = new DirectoryChoose();
            // 获取目录路径
            path = DC.getPath();
            // 获取图片路径列表
            imageList = DC.getImageList(path);
            image = new Image("file:///" + path + File.separator + imageList.get(0));
            imageView.setImage(image);
        });
        return button;
    }
    public String getCurrentPath() {
        return path;
    }
}

如果您还有其他问题,欢迎随时提问。

英文:

I want to know about sending event from child to parent.
I wrote this code, and if choosing a directory, I can see a first picture in a directory on ImageView.

After pushing a button and selecting a directory, I want to send path to ParentController.
But, now I cannot send because getCurrentPath() is called when creating a window.

ParentController
<!-- language: lang-java -->

@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
<!-- language: lang-java -->

public class OpenDirectoryButton {

    public static String path;
    public Button getDirSetImageSetListView(Button button, ImageView imageView) {
        button.setOnAction(actionEvent -&gt; {
            // 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(&quot;file:///&quot; + path + File.separator + imageList.get(0));
            imageView.setImage(image);
        });
        return button;
    }
    public String getCurrentPath() {
        return path;
    }
}

答案1

得分: 1

使用事件的方法有几种。

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

  2. 您可以声明您的类为静态字段,然后将其发送到您的子类,并且从子类中您将使用您的字段。这不是一个好的使用示例。

ParentController

在类的字段中,


public static ParentConrtroller instance; // 不好的在公共中声明字段


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

Child Controller

ParentController.instance // 并且父类的每个公共方法在这里都可以使用

英文:

For using with Events there is several ways.

1)https://github.com/AlmasB/FXEventBus. You could integrate it into your project then might to use to manipulate with events.

  1. You could declare your class like static field and send to your child and from child class you will use your fields Not good example to use.

ParentController

in field of class

public static ParentConrtroller instance; // Not good declare field in public
    
    ***
    public void inititalize(URL location, ResourceBundle resources){
     instance = this;
}

Child Controller

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

答案2

得分: 0

Pass a consumer to getDirSetIgmageSetListView

ParentController

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

ChildController

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);
        pathConsumer.accept(path); //execute consumer with your path variable
    });
    return button;
}
英文:

Pass a consumer to getDirSetIgmageSetListView

ParentController

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

ChildController

public Button getDirSetImageSetListView(Button button, ImageView imageView, Consumer&lt;String&gt; pathConsumer) {
    button.setOnAction(actionEvent -&gt; {
        // 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(&quot;file:///&quot; + path + File.separator + imageList.get(0));
        imageView.setImage(image);
        imageConsumer.accept(path); //execute consumer with your path variable
    });
    return button;
}

huangapple
  • 本文由 发表于 2020年3月15日 10:55:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/60689326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定