Search code examples
javauser-interfacejavafxactionlistener

Javafx adding ActionListener to button


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

In the code above we are defining what will happen when we press the button. This is all good but I wanna create new ActionListener and then add it to my button. Normally in JButton I can just add ActionListener like this:

button.addActionListener(someControllerClass.createButtonListener());

In code above createButtonListener() returns ActionListener.

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


Solution

  • I think this is how I should do. Creating the 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 Handler to button:

    btnSol.addEventHandler(MouseEvent.MOUSE_CLICKED, biddingHelperFrameController.createSolButtonHandler());