-
Notifications
You must be signed in to change notification settings - Fork 50
Sample Usage
J.Zimmermann edited this page Jun 26, 2015
·
3 revisions
Sample Usage:
public class JavaFXKeyboardTest extends Application {
static KeyBoardPopup popup;
private static Animation fadeAnimation;
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("FX Keyboard (" + System.getProperty("javafx.runtime.version") + ")");
// load fxml resource
Parent root = FXMLLoader.load(getClass().getResource("TestArea.fxml"));
Scene scene = new Scene(root);
// add keyboard scene listener to all text components
scene.focusOwnerProperty().addListener((value, n1, n2) -> {
if (n2 != null && n2 instanceof TextInputControl) {
setPopupVisible(true, (TextInputControl) n2);
} else {
setPopupVisible(false, null);
}
});
popup = KeyBoardPopupBuilder.create()
// initial keyboard scale
.initScale(1.1)
// path to your customized keyboard layouts
.layerPath(Paths.get("numblock"))
// choose your prefered startup locale
.initLocale(Locale.ENGLISH)
// choose swing or javafx keyboard controller
.addIRobot(new FXRobotHandler())
// customized css style
.style(getClass().getResource("blue.css").toExternalForm())
.build();
popup.getKeyBoard().setOnKeyboardCloseButton(event -> setPopupVisible(false, null));
popup.setAutoFix(true);
stage.setScene(scene);
popup.show(stage);
stage.show();
}
static void setPopupVisible(boolean visible, final TextInputControl textNode) {
if (visible) {
if (textNode != null) {
Rectangle2D textNodeBounds = new Rectangle2D(textNode.getScene().getWindow().getX() + textNode.getLocalToSceneTransform().getTx(),
textNode.getScene().getWindow().getY() + textNode.getLocalToSceneTransform().getTy(), textNode.getWidth(), textNode.getHeight());
System.out.println(textNodeBounds);
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
System.err.println(popup.getWidth());
if (textNodeBounds.getMinX() + popup.getWidth() > screenBounds.getMaxX()) {
popup.setX(screenBounds.getMaxX() - popup.getWidth());
} else {
popup.setX(textNodeBounds.getMinX());
}
if (textNodeBounds.getMaxY() + popup.getHeight() > screenBounds.getMaxY()) {
popup.setY(textNodeBounds.getMinY() - popup.getHeight() + 20);
} else {
popup.setY(textNodeBounds.getMaxY() + 40);
}
}
}
if (fadeAnimation != null) {
fadeAnimation.stop();
}
popup.getKeyBoard().setOpacity(0.0);
FadeTransition fade = new FadeTransition(Duration.seconds(.1), popup.getKeyBoard());
fade.setToValue(visible ? 1.0 : 0.0);
fade.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
fadeAnimation = null;
}
});
ScaleTransition scale = new ScaleTransition(Duration.seconds(.1), popup.getKeyBoard());
scale.setToX(visible ? 1 : 0.8);
scale.setToY(visible ? 1 : 0.8);
ParallelTransition tx = new ParallelTransition(fade, scale);
fadeAnimation = tx;
if (visible) {
if (!popup.isShowing()) {
popup.show(popup.getOwnerWindow());
}
}
tx.play();
}
public static void main(String[] args) {
launch(args);
}
}