So im having issues making a textarea draggable , with the same code for the events i can with no issues drag a textfield, but when i try it with a textarea i get no response of the System.out.println("is dragged"); on the textarea, but when i click the textarea however i do get feedback that its being clicked.
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class TextAreaDraggableDemo extends Application {
double orgSceneX, orgSceneY;
double orgTranslateX, orgTranslateY;
AnchorPane anchorpane = new AnchorPane();
TextArea textarea = new TextArea();
TextField textfield = new TextField();
BorderPane root = new BorderPane();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(root, 640, 480);
primaryStage.setScene(scene);
primaryStage.show();
textarea.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
orgSceneX = e.getSceneX();
orgSceneY = e.getSceneY();
orgTranslateX = ((TextArea) e.getSource()).getTranslateX();
orgTranslateY = ((TextArea) e.getSource()).getTranslateY();
System.out.println("is clicked");
((TextArea) (e.getSource())).toFront();
});
textarea.addEventHandler(MouseEvent.MOUSE_DRAGGED, e -> {
System.out.println("is dragged");
double offsetX = e.getSceneX() - orgSceneX;
double offsetY = e.getSceneY() - orgSceneY;
double newTranslateX = orgTranslateX + offsetX;
double newTranslateY = orgTranslateY + offsetY;
((TextArea) (e.getSource())).setTranslateX(newTranslateX);
((TextArea) (e.getSource())).setTranslateY(newTranslateY);
});
// same code as the TextArea but this works
textfield.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
orgSceneX = e.getSceneX();
orgSceneY = e.getSceneY();
orgTranslateX = ((TextField) e.getSource()).getTranslateX();
orgTranslateY = ((TextField) e.getSource()).getTranslateY();
System.out.println("is clicked");
((TextField) (e.getSource())).toFront();
});
textfield.addEventHandler(MouseEvent.MOUSE_DRAGGED, e -> {
System.out.println("is dragged");
double offsetX = e.getSceneX() - orgSceneX;
double offsetY = e.getSceneY() - orgSceneY;
double newTranslateX = orgTranslateX + offsetX;
double newTranslateY = orgTranslateY + offsetY;
((TextField) (e.getSource())).setTranslateX(newTranslateX);
((TextField) (e.getSource())).setTranslateY(newTranslateY);
});
anchorpane.getChildren().addAll(textarea, textfield);
root.setCenter(anchorpane);
}
}
The TextArea is a bit of a special case. You need to add the event handler on the content of the TextArea, but then operate on the TextArea itself instead of the event's source.
Here's a modified version of your code:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class TextAreaDraggableDemo extends Application {
double orgSceneX, orgSceneY;
double orgTranslateX, orgTranslateY;
@Override
public void start(Stage primaryStage) throws Exception {
TextArea textarea = new TextArea();
Group group = new Group();
group.getChildren().addAll(textarea);
Scene scene = new Scene(group, 640, 480);
primaryStage.setScene(scene);
primaryStage.show();
Node textAreaContent = textarea.lookup(".content");
textAreaContent.addEventHandler(MouseEvent.MOUSE_PRESSED, e -> {
System.out.println("is clicked");
orgSceneX = e.getSceneX();
orgSceneY = e.getSceneY();
orgTranslateX = textarea.getTranslateX();
orgTranslateY = textarea.getTranslateY();
textarea.toFront();
});
textAreaContent.addEventHandler(MouseEvent.MOUSE_DRAGGED, e -> {
System.out.println("is dragged");
double offsetX = e.getSceneX() - orgSceneX;
double offsetY = e.getSceneY() - orgSceneY;
double newTranslateX = orgTranslateX + offsetX;
double newTranslateY = orgTranslateY + offsetY;
textarea.setTranslateX(newTranslateX);
textarea.setTranslateY(newTranslateY);
});
}
public static void main(String[] args) {
launch(args);
}
}