M
M
Michaellux2018-05-30 10:39:31
Java
Michaellux, 2018-05-30 10:39:31

When to connect event handlers in JavaFX if event targets appear dynamically?

The program has a panel with buttons, which initially looks like this:
5b0e4ff156f10261442980.png
When the program starts, I use the method attachMonitoringEvents(), which, as planned, should register all event handlers in the program:

public ControllerMonitoringEnvironment(MonitoringEnvironment monitoringEnvironmentModel,MonitoringEnvironmentView monitoringEnvironmentView) {
        this.monitoringEnvironmentModel = monitoringEnvironmentModel;
        this.monitoringEnvironmentView = monitoringEnvironmentView;
        attachMonitoringEvents();
    }

Button addImageForActivityButton;
TextField nameOfActivityTextField;

    public void attachMonitoringEvents() {
            monitoringEnvironmentView.getAddActivityButton()
            .setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    System.out.println("Создать активность");


                    addImageForActivityButton =  new Button();

                    //...//

                    FileChooser fileChooser = new FileChooser();
                    fileChooser.setTitle("Выбор изображения для активности");

                    addImageForActivityButton.setOnAction(
                            new EventHandler<ActionEvent>() {
                                @Override
                                public void handle(ActionEvent event) {
                                    configureFileChooser(fileChooser);
                                    File file = fileChooser.showOpenDialog(stage);
                                    if (file != null) {
                                        openFile(file);
                                        attachActivityPanelsEvents();
                                        createAddActivityPanel(file);
                                    }
                                }
                            }
                    );
                }
            });


        }

This method registers a button click event handler with a plus sign.
Click on the plus button. Result:
5b0e51434ad2b079903899.png
There is a button to add an image.
The button handler for adding an image is declared in the button handler with a plus sign.
Selected image:
5b0e5167654d8052422393.png
The "OK" button appeared. We connected the "OK" button handler:
private void createAddActivityPanel(File file) {

      
        Button okChooseImageButton = new Button("OK");

       // okChooseImageButton.addEventHandler(MouseEvent.MOUSE_CLICKED,addActivityHandler);


    }

    private  void attachActivityPanelsEvents() {
            EventHandler<MouseEvent> addActivityHandler =
                    new EventHandler<MouseEvent>() {

                        @Override
                        public void handle(MouseEvent event) {
                            System.out.println("Add activity");

                        }
                    };

            nameOfActivityTextField.setOnKeyPressed(
                new EventHandler<KeyEvent>() {
                    @Override
                    public void handle(KeyEvent keyEvent) {
                        if (keyEvent.getCode() == KeyCode.ENTER) {
                            System.out.println("Add activity");


                        }
                    }
                }
        );
    }

I want addActivityHandlerto include in the method createAddActivityPanel().
Is it possible and reasonable to create/hook an event handler in another event handler?
How to correctly connect and disconnect event handlers in JavaFX if elements are added dynamically?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Alexandrov, 2018-05-30
@Michaellux

And what is the sacred meaning of poking around and creating heaps of handlers? Judging by the code, you have exactly the same result, i.e.
1) Pressed the plus sign, a picture button appeared
2) Pressed the picture button, selected a picture, the "ok" button appeared
3) The "ok" button does System.out.println("Add activity");
It follows that there is no point in fussing like that either, because it's easier to go other ways =) Which ones I recommend to think about with your own brains. I’ll just give you a hint, classes can be overridden and inherited, you can store a link to the selected object and dance from it, you can declare an EventHandler and simply give a link to the necessary components and not create and produce heaps of EventHandlers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question