This project has moved to be part of wpilibsuite/desktop-common
An alternative to JavaFX behavior classes that were placed in an internal module
in Java 9 with no publicly accessible replacement. This is not a one-to-one
replacement; the bindings API is more robust and functionally-focused;
actions are specified by lambda expressions or method references instead of
magic strings which API consumers must provide and switch
on.
- No magic strings!
- Typos are compile-time exceptions, not runtime headaches
- Easier to define multiple inputs for single actions
- e.g. define multiple sets of key presses that result in the same action
- Builder pattern for creating bindings instead of weird with-ers (
.alt()
,.shift()
, etc)
import com.sun.javafx.scene.control.behavior.BehaviorBase;
import com.sun.javafx.scene.control.behavior.KeyBinding;
public class OldBehavior extends BehaviorBase<Control> {
private static final String ACTION_NAME = "AnAction";
public OldBehavior(Control control) {
super(control, List.of(new KeyBinding(KeyCode.SPACE, ACTION_NAME)));
}
@Override
protected void callAction(String action) {
switch (action) {
case ACTION_NAME:
doAction();
break;
default:
super.callAction(action);
break;
}
}
public void doAction() {
// ...
}
}
import com.github.samcarlberg.fxbehaviors.BehaviorBase;
import com.github.samcarlberg.fxbehaviors.InputBindings;
import com.github.samcarlberg.fxbehaviors.KeyBinding;
public class NewBehavior extends BehaviorBase<Control, NewBehavior> {
private static final KeyBinding<NewBehavior> doActionOnSpace = KeyBinding.<NewBehavior>builder()
.withKey(KeyCode.SPACE)
.withAction(NewBehavior::doAction)
.build();
public NewBehavior(Control control) {
super(control, InputBindings.of(doActionOnSpace));
}
public void doAction() {
// ...
}
}