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      (() -> logInfo("Pressing the button"));
button.whilePressed(() -> logInfo("Pressing"), 1000);
button.onUp        (() -> logInfo("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

if you want to deRegister only a single function, you can do so like this:

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.