12
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 ?

MertG
  • 393
  • 1
  • 4
  • 15

3 Answers3

10

If you want to e.g. reuse an EventHandler, define it like described in JavaFX Documentation as:

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

You can now add your defined buttonHandler to the onAction of your button via:

button.setOnAction(buttonHandler);

And citing from the documentation providing the remove option for completeness:

To remove an event handler that was registered by a convenience method, pass null to the convenience method, for example, node1.setOnMouseDragged(null).

Resulting for you in:

button.setOnAction(null)

The documentation furthermore provides some examples how to add handler for specific events - it's a good read.

cagdasalagoz
  • 460
  • 1
  • 12
  • 22
SSchuette
  • 609
  • 7
  • 15
  • Ty for your answer SSchuette. If I understand you correctly you are saying I should use eventHandlers instead of actionListeners. Am I right? – MertG Nov 23 '16 at 07:51
  • As you're seeming to come from Swing (your JButton statement) you've to keep terminology correspondig to your UI framework. EDIT: Have a look at http://stackoverflow.com/a/29785487/2061026 – SSchuette Nov 23 '16 at 09:53
  • 1
    Yeah I just look for eventHandlers and its a bit different, but I solved my problem thanks for your help. – MertG Nov 23 '16 at 10:24
  • No worries @MertG - switching the UI framework takes some time. Especially if you're continuously mixing Swing, JavaFX, WPF and so on. – SSchuette Nov 23 '16 at 10:38
4

Just the same approach, but easier with lamda expressions:

button.setOnAction(event -> buttonSaveClicked());
D4NT3
  • 141
  • 7
2

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());
MertG
  • 393
  • 1
  • 4
  • 15
  • Why do you want to provide a method (for your explicit stated code example) returning a new EventHandler contrary to the approach in my answer? – SSchuette Nov 23 '16 at 09:59
  • @SSchuette In your answer you can have only one event handler at a time . The OP may need more actions to be fired so he needs multiple action handlers. – GOXR3PLUS Nov 23 '16 at 11:07
  • Yep, full ack @GoXR3Plus but that's why asked - the described scenario seemed to be "ok" with the single handler. – SSchuette Nov 23 '16 at 11:14
  • I just post an answer to my own question accourding to your suggestion @SSchuette. It is working perfectly. – MertG Nov 23 '16 at 11:39
  • Btw I didnt see your question soryy @SSchuette. I am trying to do MVC pattern. I dont want to mix view class and controller class together, so only controller knows what the button does, not the view class. – MertG Nov 23 '16 at 11:42
  • 1
    It doesn't make any sense to create multiple instances of the same EventHandler by a method like this. Do it as shown in the other answer. – Clemens Dec 05 '16 at 08:43
  • I don't like putting what button does and buttons definition in time same place. @Clemens – MertG Dec 06 '16 at 09:30
  • @MertG Not sure what you're trying to say, but having a `createSolButtonHandler()` like this is obviously nonsense. – Clemens Dec 06 '16 at 09:33
  • @Clemens I have 2 seperate class 1st one is just the GUI components and the 2nd one is a controller which controls what will buttons do, so when I code 1st class I dont like putting what button does in that class. That is controllers job. I create and determine what will they do at 2nd class and add them to 1st class. – MertG Dec 06 '16 at 10:08