JavaFX - Move borderless window by dragging an anchorpane

0

I am doing a program where I occupy a Stage undecorated, which contains an anchorPane as a title. That anchorPane I want to drag and move the window, but I can not write the event mouse pressed and mouse drag.

If anyone knows a better way to move a sale without borders, I would appreciate it.

    
asked by José Herrera Avila 10.04.2018 в 03:57
source

1 answer

0

I have made this interface:

public interface DraggedScene {

    default void onDraggedScene(AnchorPane panelFather) {
        AtomicReference<Double> xOffset = new AtomicReference<>((double) 0);
        AtomicReference<Double> yOffset = new AtomicReference<>((double) 0);

        panelFather.setOnMousePressed(e -> {
            Stage stage = (Stage) panelFather.getScene().getWindow();
            xOffset.set(stage.getX() - e.getScreenX());
            yOffset.set(stage.getY() - e.getScreenY());

        });

        panelFather.setOnMouseDragged(e -> {
            Stage stage = (Stage) panelFather.getScene().getWindow();
            stage.setX(e.getScreenX() + xOffset.get());
            stage.setY(e.getScreenY() + yOffset.get());
            panelFather.setStyle("-fx-cursor: CLOSED_HAND;");
        });

        panelFather.setOnMouseReleased(e-> panelFather.setStyle("-fx-cursor: DEFAULT;"));


    }
}

Note: The onDraggedScene method is called in the initialize method in each FXML controller after implementing the Initializable interface, this method is passed the parent panel in the fxml.

Example of its use:

public class ContainerController implements Initializable,DraggedScene {
 @FXML
 AnchorPane container;

 @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.onDraggedScene(this.container);
    }

} 
    
answered by 11.04.2018 в 15:07