There is multiple ways you can go at it.
If the parameter you want to pass along is part of the event source or target you can:
- use getSource or getTarget
- check the class of source or target
- cast the source/target to that class
- access parameters via the getters and setters of that class
However if the parameter has nothing to do with the event, you will have to write a custom event:
class CustomEvent extends Event {
private String parameter;
public static final EventType<CustomEvent> CUSTOM = new EventType(ANY, "CUSTOM");
public CustomEvent(String parameter) {
super(CustomEvent.CUSTOM);
this.parameter = parameter;
}
public String getParameter() {
return this.parameter;
}
}
Now to use that event you will first have to fire it.
You can do this with
objectThatWillFireThisEvent.fireEvent(new CustomEvent("Get this parameter guys!"));
So you now fired an event with a parameter. Now to set the EventHandler simple add this to the class of the object that will fire the event:
public final void setOnCustomEvent(
EventHandler<? super CustomEvent> value) {
this.addEventHandler(CustomEvent.CUSTOM, value);
}
Now you can set the event handler:
objectThatWillFireTheEvent.setOnCustomEvent( event -> {
System.out.println(event.getParameter());
});
Alternatively if want you can use the way to write event handlers that you posted in your question (if you don't want to use lambdas).
Or you can just call a function that you wrote, that should handle that parameter:
objectThatWillFireTheEvent.setOnCustomEvent( event -> myFunction(event.getParameter) );
I hope I didn't make any typos. But if something does not work or you have any more questions, don't hesitate to ask! :)
In addition to that I would advice you to google casting (if you do not know that already) and more on custom events (since my answer is only a starting point and if you want to do more crazy shit with custom events it's better to read up on that :D)
Edit:
Is this what you meant in your comment?
Since comments will destroy layout and readability
String text = "This is text!";
Button button = new Button();
object.setOnMouseClicked( event -> {
function1(text);
function2(button);
});