Javafx Adds ActionListener to Button

button.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent e) {
        label.setText("Accepted");
    }
});

In the above code, we determine what happens when we press the button. This is all well and good, but I want to create a new ActionListener and then add it to my button. Usually in JButton I can simply add an ActionListener as follows:

button.addActionListener(someControllerClass.createButtonListener());

In the code above, createButtonListener () returns an ActionListener.

My question is: what is the equivalent of JButton addActionListener?

+4
source share
2 answers

I think that is how I should do it. Create a handler:

public EventHandler<Event> createSolButtonHandler()
{
    btnSolHandler = new EventHandler<Event>() {

        @Override
        public void handle(Event event) {
            System.out.println("Pressed!");
            biddingHelperFrame.getBtnSag().setVisible(false);
        }
    };
    return btnSolHandler;
}

Adding a handler to a button:

btnSol.addEventHandler(MouseEvent.MOUSE_CLICKED, biddingHelperFrameController.createSolButtonHandler());
+1
source

, , EventHandler, , JavaFX :

EventHandler<ActionEvent> buttonHandler = new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        label.setText("Accepted");
        event.consume();
    }
};

buttonHandler onAction :

button.setOnAction(buttonHandler);

, :

, , null , node1.setOnMouseDragged(null).

:

button.setOnAction(null)

- .

+4

Source: https://habr.com/ru/post/1661675/


All Articles