Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created August 15, 2021 13:45
Show Gist options
  • Save Da9el00/74cc0100b67ade75308f3875c2c681cb to your computer and use it in GitHub Desktop.
Save Da9el00/74cc0100b67ade75308f3875c2c681cb to your computer and use it in GitHub Desktop.
JavaFX and Scene Builder - Make Components Draggable
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Rectangle;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private Rectangle rectangle;
@FXML
private AnchorPane anchorPane;
DraggableMaker draggableMaker = new DraggableMaker();
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
draggableMaker.makeDraggable(rectangle);
draggableMaker.makeDraggable(anchorPane);
}
}
package sample;
import javafx.scene.Node;
public class DraggableMaker {
private double mouseAnchorX;
private double mouseAnchorY;
public void makeDraggable(Node node){
node.setOnMousePressed(mouseEvent -> {
mouseAnchorX = mouseEvent.getX();
mouseAnchorY = mouseEvent.getY();
});
node.setOnMouseDragged(mouseEvent -> {
node.setLayoutX(mouseEvent.getSceneX() - mouseAnchorX);
node.setLayoutY(mouseEvent.getSceneY() - mouseAnchorY);
});
}
}
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Rectangle?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Rectangle fx:id="rectangle" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="100.0" layoutX="285.0" layoutY="243.0" stroke="BLACK" strokeType="INSIDE" width="100.0" />
<AnchorPane fx:id="anchorPane" layoutX="100.0" layoutY="26.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #374829;">
<children>
<Button layoutX="33.0" layoutY="41.0" mnemonicParsing="false" text="Button" />
<Button layoutX="115.0" layoutY="41.0" mnemonicParsing="false" text="Button" />
<TextField layoutX="26.0" layoutY="143.0" />
<RadioButton layoutX="92.0" layoutY="97.0" mnemonicParsing="false" />
</children>
</AnchorPane>
</children>
</AnchorPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment