Answer the question
In order to leave comments, you need to log in
When to connect event handlers in JavaFX if event targets appear dynamically?
The program has a panel with buttons, which initially looks like this:
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);
}
}
}
);
}
});
}
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");
}
}
}
);
}
addActivityHandler
to include in the method createAddActivityPanel()
. Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question