Simple Button


The SimpleButton is a template class, that you can use in your own Java-project.

The Template Class gives you the option to check the state of the button, and to create simple events if the button is pressed, depressed or while it is being pressed.


Simple Button Layout


A simple example on how to use the Button-Class from the Hardware-Catalog:

// Initialize the button component
final var button = new SimpleButton(pi4j, PIN.D26, Boolean.FALSE);

 // Register event handlers to print a message when pressed (onDown) and depressed (onUp)
 button.onDown      (() -> System.out.println("Button pressed"));
 button.whilePressed(() -> System.out.println("Still pressing"), Duration.ofSeconds(1));
 button.onUp        (() -> System.out.println("Stopped pressing"));

 // Wait for 15 seconds while handling events before exiting
 System.out.println("Press the button to see it in action!");

 // Unregister all event handlers to exit this application in a clean way

Further application

The class is implemented in the two sample projects Theremin and Photobooth.

Further project ideas

  • An application, which includes a button. if the button is pressed, the app will order you a crate of beer from your favorite store.
  • An application, which includes a buzzer and a button. If the button is pressed, the buzzer beeps.