Как отправить событие из дочернего класса в родительский класс - PullRequest
0 голосов
/ 15 марта 2020

Я хочу знать об отправке события от ребенка к родителю. Я написал этот код, и, выбирая каталог, я вижу первое изображение в каталоге в ImageView.

После нажатия кнопки и выбора каталога я хочу отправить путь в ParentController. Но теперь я не могу отправить, потому что при создании окна вызывается getCurrentPath ().

ParentController

@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;
    }
}

Ответы [ 2 ]

2 голосов
/ 16 марта 2020

Для использования с событиями есть несколько способов.

1) https://github.com/AlmasB/FXEventBus. Вы можете интегрировать его в свой проект, а затем использовать для манипулирования событиями.

2) Вы можете объявить свой класс как поле stati c и отправить своему ребенку, а из дочернего класса вы будете использовать свои поля Не хороший пример для использования.

ParentController

в поле класса

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 
0 голосов
/ 16 марта 2020

Передача потребителю 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);
        imageConsumer.accept(path); //execute consumer with your path variable
    });
    return button;
}
...