Javafx button sending arguments to ActionEvent function

可紊 提交于 2019-12-23 12:42:59

问题


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?


回答1:


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) {
        // ...
    }
}



回答2:


Just implement EventHandler<ActionEvent> or create an EventHandler<ActionEvent> anonyme class in your class and call on each button you want setOnAction(myAction);



来源:https://stackoverflow.com/questions/37902660/javafx-button-sending-arguments-to-actionevent-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!