Skip to content

Instantly share code, notes, and snippets.

@MrZoraman
Last active October 16, 2015 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrZoraman/015d46463749e5a3e31b to your computer and use it in GitHub Desktop.
Save MrZoraman/015d46463749e5a3e31b to your computer and use it in GitHub Desktop.
All the ways to pass 'blocks of code' to methods in java.
package javafxapplication1;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class JavaFXApplication1 extends Application
{
/*
Here are all the ways to assign a handler to a button via the
setOnAction() method. They are listed in order from
most verbose to least. Every way does the exact same thing.
*/
@Override
public void start(Stage primaryStage)
{
VBox root = new VBox();
/*
Implement the EventHandler and then pass an instance
of that class to the button.
*/
Button button1 = new Button("Button 1");
class EventHandlerImpl implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent event)
{
System.out.println("Hello from button 1!");
}
}
EventHandlerImpl handler = new EventHandlerImpl();
button1.setOnAction(handler);
root.getChildren().add(button1);
/*
Create an anonymous class that implements the EventHandler
and pass that to the button.
*/
Button button2 = new Button("Button 2");
EventHandler<ActionEvent> anonEventHandler = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event)
{
System.out.println("Hello from button 2!");
}
};
button2.setOnAction(anonEventHandler);
root.getChildren().add(button2);
/*
Create the anonymous class and pass it straight into the button's
setOnAction() method. This is what the default project template does.
*/
Button button3 = new Button("Button 3");
button3.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event)
{
System.out.println("Hello from button 3!");
}
});
root.getChildren().add(button3);
/*
Use the closest thing java has to method poiters. The jvm makes
the method passed in act like an instance of the EventHandler
interface. As long as the interface only has a single method,
and that method signature matches the signature of the method
you pass into the setOnAction() method, it will work.
*/
Button button4 = new Button("Button 4");
button4.setOnAction(this::onClick);
root.getChildren().add(button4);
/*
Use a lambda (multiple lines) expression. The stuff in the parentheses is the arguments
of the handle() method. Their types are inferred so they are not
declared like they are everywhere else in java. This method only
works if the same conditions mentioned in button 4 are met.
*/
Button button5 = new Button("Button 5");
button5.setOnAction((event) -> {
System.out.println("Hello from button 5!");
});
root.getChildren().add(button5);
/*
Same as button 5, but if only a single line is executed, the curly
braces can be omitted.
*/
Button button6 = new Button("Button 6");
button6.setOnAction((event) -> System.out.println("Hello from button 6!"));
root.getChildren().add(button6);
/*
Same as button 6, but if there is only one parameter in the method
signature, the parentheses around the argument can be omitted.
Note that if there are no parameters in the method signature,
An empty set of parenthese () are required.
*/
Button button7 = new Button("Button 7");
button7.setOnAction(event -> System.out.println("Hello from button 7!"));
root.getChildren().add(button7);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
private void onClick(ActionEvent e)
{
System.out.println("Hello from button 4!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment