What is the most suitable way to pass multiple parameters to EventHandler of controller class?
I am developing a calendar modal for an application I have developed. My requirement is to pass month and year separately to the controller. This is my current approach.
FXML :
<Button onAction="#handleClicks" text="DECEMBER" textFill="WHITE" userData="2018/12">
Controller :
public class CalendarController implements Initializable {
public void handleMonths(ActionEvent ev){
Node node = (Node) ev.getSource();
String full_mnth = node.getUserData().toString();
String[] date = full_mnth.split("/");
System.out.println("Year : "+date[0] +" Month : "+date[1]);
}
}
But it would be easier if I could use an array as UserData or if I could pass multiple parameters. Can someone please suggest the most appropriate way to achieve this.
Use the Node.properties
map. This allows you to store objects using a string as key. Just be sure not to use keys similar to the static
properties of the parent (refering to e.g. AnchorPane.leftAnchor
here), as those properties are stored in this map too:
<Button onAction="#handleClicks" text="DECEMBER" textFill="WHITE">
<properties>
<year>
<Integer fx:value="2018"/>
</year>
<month>
<Integer fx:value="12"/>
</month>
</properties>
</Button>
@FXML
private void handleClicks(ActionEvent event) {
Map<Object, Object> properties = ((Node) event.getSource()).getProperties();
System.out.println("year: "+properties.get("year"));
System.out.println("month: "+properties.get("month"));
}