Java Reference
In-Depth Information
javafx.scene.input.KeyCombination
classes. Seeing that the previous re-
cipe was a tad boring, we decided to make things a little more interesting here. This re-
cipe displays
Text
nodes onto the scene graph when the user performs the key com-
binations. When displaying the
Text
nodes, we applied an inner shadow effect. The
following code snippet creates a
Text
node with an inner shadow effect:
InnerShadow iShadow = new InnerShadow();
iShadow.setOffsetX(3.5f);
iShadow.setOffsetY(3.5f);
final Text status = new Text();
status.setEffect(iShadow);
status.setX(100);
status.setY(50);
status.setFill(Color.LIME);
status.setFont(Font.font(null, FontWeight.BOLD, 35));
status.setTranslateY(50);
To create a keyboard shortcut, you simply call a menu or button control's
setAc-
celerator()
method. In this recipe, the key combination are set using the
MenuItem
node's
setAccelerator()
method. The following code line specifies
the key combinations for a Ctrl-N:
MenuItem newItem = new MenuItem();
newItem.setText("New");
newItem.setAccelerator(new KeyCodeCombination(KeyCode.N,
KeyCombination.CONTROL_DOWN));
newItem.setOnAction((e) -> {
statusProperty.set("Ctrl-N");
});
As you can see from the code, when the accelerator (key combination) is pressed in
the example, the
onAction ActionEvent
is triggered. It sets the
statusProp-
erty
value to Ctrl-N via a lambda expression.
14-13. Creating and Working with Tables