How to get the X and Y location of a javaFX node for displaying a PopupControl?

Jeremiah Binegar :

I have created a JavaFX class that is similar to the DatePicker class but instead displays a popup with a way to choose a time. I am wanting to have the popup appear underneath the TextField and Button just as the DatePicker Popup does, but I am unable to find a way to get the X and Y coordinates of the Nodes. Any help would be appreciated.

jewelsea :

You can get screen co-ordinates using: node.localToScreen(x,y).

The following code will locate (anchor) the top left corner of a pop-up window at the bottom center of a node on the pop-up owner window:

Point2D anchorPoint = node.localToScreen(
        node.getWidth() / 2,
        node.getHeight()
);

popup.setAnchorLocation(
        PopupWindow.AnchorLocation.WINDOW_TOP_LEFT
);

popup.show(
        node,
        anchorPoint.getX(),
        anchorPoint.getY()
);

Sample application:

Displays and hides a popup located at the bottom center of controlling button.

third law

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.stage.*;

public class Popcorn extends Application {
    @Override
    public void start(Stage stage) {
        StackPane popupLayout = new StackPane(
                new Label(
                        "Clarke's third law\nAny sufficiently advanced technology is indistinguishable from magic."
                )
        );
        popupLayout.setStyle("-fx-opacity: 0.8; -fx-background-color: paleturquoise;");
        popupLayout.setPadding(new Insets(10));

        Popup popup = new Popup();
        popup.getContent().add(
                popupLayout
        );
        popupLayout.setOnMouseClicked(event -> popup.hide());

        ToggleButton showPopupButton = new ToggleButton("Show popup");

        showPopupButton.textProperty().bind(
                Bindings.when(showPopupButton.selectedProperty())
                        .then("Hide popup")
                        .otherwise("Show popup")
        );

        showPopupButton.selectedProperty().addListener((observable, wasSelected, isSelected) -> {
            if (isSelected) {
                showPopup(popup, showPopupButton);
            } else {
                popup.hide();
            }
        });

        popup.setOnShown(event -> showPopupButton.setSelected(true));
        popup.setOnHidden(event -> showPopupButton.setSelected(false));

        StackPane stageLayout = new StackPane(showPopupButton);
        stageLayout.setPadding(new Insets(10));
        stage.setScene(new Scene(stageLayout));
        stage.show();
    }

    private void showPopup(Popup popup, Control ownerNode) {
        Point2D anchorPoint = ownerNode.localToScreen(
                ownerNode.getWidth() / 2,
                ownerNode.getHeight()
        );

        popup.setAnchorLocation(
                PopupWindow.AnchorLocation.WINDOW_TOP_LEFT
        );

        popup.show(
                ownerNode,
                anchorPoint.getX(),
                anchorPoint.getY()
        );
    }

    public static void main(String[] args) {
        launch(args);
    }
}

A note on popup anchors

The popup anchor point is just advice to the popup system on where to place the popup. The popup logic built into JavaFX is smart enough to know the size of the popup window and its initial location on the screen. If that means that there is not enough real estate left on the screen to display the popup at the suggested anchor point, the popup implementation will automatically adjust the anchor point to ensure that the popup will be entirely visible when first displayed. You can try this out by running the example code and placing the owner stage at the extreme bottom or right of the screen before trying to show the popup.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=357663&siteId=1