Created
September 23, 2013 17:22
-
-
Save jeffreyguenther/6673953 to your computer and use it in GitHub Desktop.
Draggable Issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package javafxtest; | |
/** | |
* Info needed to properly drag a node | |
* A copy of http://docs.oracle.com/javafx/2/events/DraggablePanelsExample.java.htm | |
* @author jeffreyguenther | |
*/ | |
public class DragContext { | |
private double mouseAnchorX; | |
private double mouseAnchorY; | |
private double initialTranslateX; | |
private double initialTranslateY; | |
public DragContext() { | |
this(0, 0, 0, 0); | |
} | |
public DragContext(double mouseAnchorX, double mouseAnchorY, double initialTranslateX, double initialTranslateY) { | |
this.mouseAnchorX = mouseAnchorX; | |
this.mouseAnchorY = mouseAnchorY; | |
this.initialTranslateX = initialTranslateX; | |
this.initialTranslateY = initialTranslateY; | |
} | |
public double getDragDestX(double destX){ | |
return initialTranslateX + destX - mouseAnchorX; | |
} | |
public double getDragDestY(double destY){ | |
return initialTranslateY + destY - mouseAnchorY; | |
} | |
/** | |
* Get the value of initialTranslateY | |
* | |
* @return the value of initialTranslateY | |
*/ | |
public double getInitialTranslateY() { | |
return initialTranslateY; | |
} | |
/** | |
* Set the value of initialTranslateY | |
* | |
* @param initialTranslateY new value of initialTranslateY | |
*/ | |
public void setInitialTranslateY(double initialTranslateY) { | |
this.initialTranslateY = initialTranslateY; | |
} | |
/** | |
* Get the value of initialTranslateX | |
* | |
* @return the value of initialTranslateX | |
*/ | |
public double getInitialTranslateX() { | |
return initialTranslateX; | |
} | |
/** | |
* Set the value of initialTranslateX | |
* | |
* @param initialTranslateX new value of initialTranslateX | |
*/ | |
public void setInitialTranslateX(double initialTranslateX) { | |
this.initialTranslateX = initialTranslateX; | |
} | |
/** | |
* Get the value of mouseAnchorY | |
* | |
* @return the value of mouseAnchorY | |
*/ | |
public double getMouseAnchorY() { | |
return mouseAnchorY; | |
} | |
/** | |
* Set the value of mouseAnchorY | |
* | |
* @param mouseAnchorY new value of mouseAnchorY | |
*/ | |
public void setMouseAnchorY(double mouseAnchorY) { | |
this.mouseAnchorY = mouseAnchorY; | |
} | |
/** | |
* Get the value of mouseAnchorX | |
* | |
* @return the value of mouseAnchorX | |
*/ | |
public double getMouseAnchorX() { | |
return mouseAnchorX; | |
} | |
/** | |
* Set the value of mouseAnchorX | |
* | |
* @param mouseAnchorX new value of mouseAnchorX | |
*/ | |
public void setMouseAnchorX(double mouseAnchorX) { | |
this.mouseAnchorX = mouseAnchorX; | |
} | |
@Override | |
public String toString() { | |
return "DragContext{" + "mouseAnchorX=" + mouseAnchorX + ", mouseAnchorY=" + mouseAnchorY + ", initialTranslateX=" + initialTranslateX + ", initialTranslateY=" + initialTranslateY + '}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package javafxtest; | |
import javafx.application.Application; | |
import static javafx.application.Application.launch; | |
import javafx.beans.property.SimpleDoubleProperty; | |
import javafx.event.EventHandler; | |
import javafx.geometry.Insets; | |
import javafx.scene.Group; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.ScrollPane; | |
import javafx.scene.control.TextField; | |
import javafx.scene.input.ContextMenuEvent; | |
import javafx.scene.input.MouseEvent; | |
import javafx.scene.layout.BorderPane; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
/** | |
* | |
* @author jeffreyguenther | |
*/ | |
public class NodeLayout extends Application { | |
public static void main(String[] args) { | |
launch(args); | |
} | |
@Override | |
public void start(Stage stage) throws Exception { | |
stage.setTitle("NodeLayout"); | |
// create a node on screen | |
Group node = createNodeControl(); | |
Group sceneRoot = new Group(node); | |
// create the scene | |
Scene scene = new Scene(sceneRoot, 800.0, 600.0); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
private void createPortControl(VBox ports) { | |
HBox port = new HBox(); | |
port.setPadding(Insets.EMPTY); | |
port.setSpacing(5); | |
ports.getChildren().add(port); | |
Label portName = new Label("Port Name"); | |
portName.setPrefWidth(100); | |
TextField portExpression = new TextField(); | |
port.getChildren().addAll(portName, portExpression); | |
} | |
private Group createNodeControl() { | |
final DragContext dragContext = new DragContext(); | |
final BorderPane container = new BorderPane(); | |
container.setStyle("-fx-background-color: lavender"); | |
HBox titleBar = new HBox(); | |
titleBar.setStyle("-fx-background-color: lightgray;"); | |
titleBar.setPadding(new Insets(5, 5, 5, 5)); | |
titleBar.setOnMouseClicked(new EventHandler<MouseEvent>() { | |
@Override | |
public void handle(MouseEvent t) { | |
System.out.println("title bar clicked"); | |
} | |
}); | |
Label nodeName = new Label("Node Name"); | |
titleBar.getChildren().add(nodeName); | |
VBox ports = new VBox(); | |
ports.setPadding(new Insets(5, 5, 5, 5)); | |
ports.setSpacing(10); | |
createPortControl(ports); | |
createPortControl(ports); | |
createPortControl(ports); | |
createPortControl(ports); | |
createPortControl(ports); | |
container.setTop(titleBar); | |
container.setCenter(ports); | |
container.setOnMousePressed( new EventHandler<MouseEvent>() { | |
@Override | |
public void handle(MouseEvent me) { | |
dragContext.setMouseAnchorX(me.getX()); | |
dragContext.setMouseAnchorY(me.getY()); | |
dragContext.setInitialTranslateX(container.getTranslateX()); | |
dragContext.setInitialTranslateY(container.getTranslateY()); | |
System.out.println("To initialize " + dragContext); | |
} | |
}); | |
container.setOnMouseDragged( new EventHandler<MouseEvent>() { | |
@Override | |
public void handle(MouseEvent me) { | |
System.out.println("Before move: " + container.getTranslateX() + ", " + container.getTranslateY()); | |
container.setTranslateX(dragContext.getInitialTranslateX() | |
+ me.getX() - dragContext.getMouseAnchorX()); | |
container.setTranslateY(dragContext.getInitialTranslateY() | |
+ me.getY() - dragContext.getMouseAnchorY()); | |
System.out.println("After move: " + container.getTranslateX() + ", " + container.getTranslateY()); | |
} | |
}); | |
Group wrapper = new Group(container); | |
return wrapper; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment