Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javafx button sending arguments to ActionEvent function

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?

like image 619
Foo Avatar asked Jun 18 '16 23:06

Foo


People also ask

Can a Button fire an ActionEvent?

A Button can fire an ActionEvent. A Button can fire a MouseEvent. A Button can fire a KeyEvent. A TextField can fire an ActionEvent.

What method do you call to register an event action handler with a Button control?

The setOnAction() method is used to register an event handler that handles the action events that are dispatched when the button is clicked.

How do I program a Button in JavaFX?

You create a button control by creating an instance of the Button class. Here is a JavaFX Button instantiation example: Button button = new Button("My Label"); The text to be displayed on the button is passed as parameters to the Button constructor.


1 Answers

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) {
        // ...
    }
}
like image 181
James_D Avatar answered Sep 20 '22 22:09

James_D