Skip to content
This repository has been archived by the owner on Sep 19, 2018. It is now read-only.

SamCarlberg/fxbehaviors

Repository files navigation

This project has moved to be part of wpilibsuite/desktop-common

FX Behaviors

Download

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.

Improvements vs JavaFX API

  • 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)

Defining Custom Behaviors

Old

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() {
    // ...
  }
  
}

New

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() {
    // ...
  }
}

About

JavaFX control behaviors for Java 9

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published