Reputation: 1678
I have this undecorated window:
public static void initStartPage(final Stage primaryStage) {
final Stage startPage = new Stage();
startPage.initStyle(StageStyle.UNDECORATED);
//startPage.initOwner(primaryStage);
//startPage.toFront();
Scene scene = new Scene(agentsPanel(), 900, 500);
startPage.setScene(scene);
startPage.show();
}
I would like to know how I can make it a draggable undecorated window? I want to change its position when the user selects the window with the right mouse button and then move the mouse while keeping the mouse button pressed down.
P.S. I tested this solution, but it's not working:
private static FlowPane flow;
private static BorderPane bpi;
public static void initStartPage(final Stage primaryStage) {
final Stage startPage = new Stage();
startPage.initStyle(StageStyle.UNDECORATED);
startPage.initOwner(primaryStage);
//startPage.toFront();
Scene scene = new Scene(agentsPanel(primaryStage), 900, 500);
startPage.setScene(scene);
startPage.show();
}
private static double xOffset = 0;
private static double yOffset = 0;
public static BorderPane agentsPanel(final Stage primaryStage) {
BorderPane bp = new BorderPane();
bp.setPrefSize(900, 500);
bp.setMaxSize(900, 500);
HBox thb = new HBox(10); // Set spacing between each child into the HBox
thb.setPadding(new Insets(15, 15, 15, 15));
HBox bhb = new HBox(10); // Set spacing between each child into the HBox
bhb.setPadding(new Insets(15, 15, 15, 15));
bp.setTop(thb);
bp.setBottom(bhb);
bp.setCenter(navigationPanel());
bp.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
xOffset = event.getSceneX();
yOffset = event.getSceneY();
}
});
bp.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
primaryStage.setX(event.getScreenX() - xOffset);
primaryStage.setY(event.getScreenY() - yOffset);
}
});
return bp;
}
Upvotes: 21
Views: 23059
Reputation: 60923
The @Eeliyaanswer working well. However, code will shorter if using the below code because we don't need to declare variables.
We need to setOnMouseDragged
again and again whenever setOnMouseDragged
fired but I think it's not causing the problem.
bp.setOnMousePressed(pressEvent -> {
bp.setOnMouseDragged(dragEvent -> {
primaryStage.setX(dragEvent.getScreenX() - pressEvent.getSceneX());
primaryStage.setY(dragEvent.getScreenY() - pressEvent.getSceneY());
});
});
Hope it helps
Upvotes: 12
Reputation: 191
you can use this, works for a stage as well as an Alert (basically another stage but for pup po messages). the node is the main node of that stage
//Make a Stage Draggable
public void DraggableStage(Node node, Stage stage, Alert alert ) {
double[] xOffset = {0}, yOffset = {0};
node.setOnMousePressed(event -> {
if (stage != null && alert == null){
xOffset[0] = stage.getX() - event.getScreenX();
yOffset[0] = stage.getY() - event.getScreenY();
} else if(stage == null && alert != null){
xOffset[0] = alert.getX() - event.getScreenX();
yOffset[0] = alert.getY() - event.getScreenY();
}
});
node.setOnMouseDragged(event -> {
if (stage != null && alert == null){
stage.setX(event.getScreenX() + xOffset[0]);
stage.setY(event.getScreenY() + yOffset[0]);
} else if(stage == null && alert != null){
alert.setX(event.getScreenX() + xOffset[0]);
alert.setY(event.getScreenY() + yOffset[0]);
}
});
}
Upvotes: 0
Reputation: 1574
Just change your setOnMousePressed method to this:
bp.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
xOffset = primaryStage.getX() - event.getScreenX();
yOffset = primaryStage.getY() - event.getScreenY();
}
});
and your setOnMouseDragged to this:
bp.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
primaryStage.setX(event.getScreenX() + xOffset);
primaryStage.setY(event.getScreenY() + yOffset);
}
});
Upvotes: 45