Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...