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) {
// ...
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…