Javafx button sending arguments to ActionEvent function
Asked Answered
C

1

5

I'm learning how to use javafx, along with scenebuilder by creating a very basic calculator but I've come across a problem where it's hard for me to find a way to not have a seperate function for each button. I need to know if there's a way for a button to change a variable or give arguments to the action event method that the button is linked to so I don't need seperate methods for each button to call another function. This is an example of what I'm working with.

@FXML
private Button one;
private Button two;
//both buttons are linked to NumChange

private void NumChange(ActionEvent e) {
    //update the number to preform an operation with here
}

How do I have all the number buttons use the same function but send it arguments so it can do different things?

All of the answers I've found involve using getting the text of the button, however my calculator is weird and I'm having the spelled out word on them so I can't do that, what can I do?

Certificate answered 18/6, 2016 at 23:53 Comment(2)
one.setOnAction(event -> NumChange(...)); two.setOnAction(event -> NumChange(...));Sublet
@Certificate Please use standard naming conventionsPrintery
P
24

The best way, if you are registering the handlers in FXML, is really to define a different method for each button. You can minimize the code as follows.

Define your numChange(...) method to take the parameters it needs, e.g.

private void numChange(int num) {
    // process num here...
}

and then define a different handler method for each button that simply delegates to numChange():

@FXML
private void handleButtonOne() {
    numChange(1);
}

@FXML
private void handleButtonTwo() {
    numChange(2);
}

and then make the obvious change in the FXML:

<Button fx:id="one" onAction="#handleButtonOne" ... />
<Button fx:id="two" onAction="#handleButtonTwo" ... />

You can alter the number and/or type of parameters that numChange() accepts as needed, of course.


Another FXML-based solution, which avoids each button having a dedicated method, is to associate values with nodes in FXML by setting their user data:

<Button fx:id="one" onAction="#numChange" userData="1" ... />
<Button fx:id="two" onAction="#numChange" userData="2" ... />

However this just results in a lot of downcasting and type conversion, which imho makes it less useful than the previous solution:

@FXML
private void numChange(ActionEvent event) {
    Node node = (Node) event.getSource() ;
    String data = (String) node.getUserData();
    int value = Integer.parseInt(data);

    // ...
}

In the end, it might just be easier to register the handlers directly in the controller:

<Button fx:id="one" ... />
<Button fx:id="two" ... />

and

public class Controller {

    @FXML
    private Button one ;

    @FXML
    private Button two ;

    public void initialize() {
        one.setOnAction(e -> numChange(1));
        two.setOnAction(e -> numChange(2));
    }

    private void numChange(int value) {
        // ...
    }
}
Printery answered 19/6, 2016 at 0:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.