JavaFX TextArea 从坐标获取插入符位置
JavaFX TextArea get caret position from coordinate
是否可以在 JavaFX TextArea
中获取屏幕坐标 (2 double
) 处的插入符号位置 (an int
)?
您可以在拖动处理程序中使用 TextAreaSkin 的 getInsertionPoint 方法:
TextAreaSkin skin = (TextAreaSkin) target.getSkin();
int insertionPoint = skin.getInsertionPoint(event.getX(), event.getY());
target.positionCaret( insertionPoint);
然而,皮肤 class 在 com.sun.javafx.* 中,所以随着 Java 9 的出现,你可能需要做一些不同的事情。没有人知道他们会破坏什么或他们提供什么作为替代品。但是,使用 Java 8 它可以工作(目前)。
您可以将 Label 的文本拖到 TextArea 中的任何位置的完整示例:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import com.sun.javafx.scene.control.skin.TextAreaSkin;
// Parts of this drag/drop example are from https://docs.oracle.com/javafx/2/drag_drop/HelloDragAndDrop.java.html
public class TextAreaDemo extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
Label source = new Label( "Draggable Text");
TextArea target = new TextArea();
target.setPrefRowCount(10);
target.setPrefColumnCount(100);
target.setWrapText(true);
target.setPrefWidth(150);
String cssDefault = "Lorem ipsum dolor sit amet, et bonorum pertinacia est, verear temporibus definitionem nam an, ius cu justo legimus philosophia. Adversarium complectitur at sit, his ex sumo nibh consequuntur. Et vim adhuc mnesarchum, eum in ignota integre tincidunt. Erant oblique alterum no eos.";
target.setText(cssDefault);
HBox root = new HBox();
root.setSpacing(10);
HBox.setMargin(source, new Insets(10,10,10,10));
HBox.setMargin(target, new Insets(10,10,10,10));
root.getChildren().addAll( source, target);
Scene scene = new Scene(root, 600, 330, Color.WHITE);
primaryStage.setScene(scene);
primaryStage.show();
source.setOnDragDetected(new EventHandler <MouseEvent>() {
public void handle(MouseEvent event) {
/* allow any transfer mode */
Dragboard db = source.startDragAndDrop(TransferMode.ANY);
/* put a string on dragboard */
ClipboardContent content = new ClipboardContent();
content.putString(source.getText());
db.setContent(content);
event.consume();
}
});
target.setOnDragOver(new EventHandler <DragEvent>() {
public void handle(DragEvent event) {
/* accept it only if it is not dragged from the same node
* and if it has a string data */
if (event.getGestureSource() != target &&
event.getDragboard().hasString()) {
/* allow for both copying and moving, whatever user chooses */
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
// position caret at drag coordinates
TextAreaSkin skin = (TextAreaSkin) target.getSkin();
int insertionPoint = skin.getInsertionPoint(event.getX(), event.getY());
target.positionCaret( insertionPoint);
}
event.consume();
}
});
target.setOnDragDropped(new EventHandler <DragEvent>() {
public void handle(DragEvent event) {
/* if there is a string data on dragboard, read it and use it */
Dragboard db = event.getDragboard();
boolean success = false;
if (db.hasString()) {
target.insertText( target.getCaretPosition(), db.getString());
success = true;
}
/* let the source know whether the string was successfully
* transferred and used */
event.setDropCompleted(success);
event.consume();
}
});
}
}
import com.sun.javafx.scene.control.skin.TextAreaSkin;
import com.sun.javafx.scene.text.HitInfo;
/****************************************************/
TextAreaSkin skin = (TextAreaSkin) target.getSkin();
HitInfo mouseHit = skin.getIndex(e.getX(), e.getY());
// Now you can position caret
skin.positionCaret(mouseHit, false, false);
// And/or get insertion index
int insertionPoint = mouseHit.getInsertionIndex();
对于TextArea
这个方法等同于罗兰的回答。这种方法的实际区别在于它适用于TextField
(TextInputControl
的另一个子class):
TextFieldSkin skin = (TextFieldSkin) target.getSkin();
HitInfo mouseHit = skin.getIndex(e.getX(), e.getY());
skin.positionCaret(mouseHit, false);
int insertionPoint = mouseHit.getInsertionIndex();
不幸的是,TextFieldSkin
不会覆盖 getInsertionPoint(...)
和父级的实现 returns 0,因此替代解决方案在这里不起作用。
关于Java9,罗兰和我的回答仍然有效。
软件包 com.sun.javafx.scene.control.skin
和 com.sun.javafx.scene.text
(HitInfo
class 所在的位置)正在移至 Java 9 中的 public API。它们的位置分别是 javafx.scene.control.skin
和 javafx.scene.text
。在此处查看 JavaFX 9 的 Java 文档:http://download.java.net/java/jdk9/jfxdocs/index.html
要始终看到光标,请将其放入 setOnDragOver
-方法
target.requestFocus();
是否可以在 JavaFX TextArea
中获取屏幕坐标 (2 double
) 处的插入符号位置 (an int
)?
您可以在拖动处理程序中使用 TextAreaSkin 的 getInsertionPoint 方法:
TextAreaSkin skin = (TextAreaSkin) target.getSkin();
int insertionPoint = skin.getInsertionPoint(event.getX(), event.getY());
target.positionCaret( insertionPoint);
然而,皮肤 class 在 com.sun.javafx.* 中,所以随着 Java 9 的出现,你可能需要做一些不同的事情。没有人知道他们会破坏什么或他们提供什么作为替代品。但是,使用 Java 8 它可以工作(目前)。
您可以将 Label 的文本拖到 TextArea 中的任何位置的完整示例:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import com.sun.javafx.scene.control.skin.TextAreaSkin;
// Parts of this drag/drop example are from https://docs.oracle.com/javafx/2/drag_drop/HelloDragAndDrop.java.html
public class TextAreaDemo extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
Label source = new Label( "Draggable Text");
TextArea target = new TextArea();
target.setPrefRowCount(10);
target.setPrefColumnCount(100);
target.setWrapText(true);
target.setPrefWidth(150);
String cssDefault = "Lorem ipsum dolor sit amet, et bonorum pertinacia est, verear temporibus definitionem nam an, ius cu justo legimus philosophia. Adversarium complectitur at sit, his ex sumo nibh consequuntur. Et vim adhuc mnesarchum, eum in ignota integre tincidunt. Erant oblique alterum no eos.";
target.setText(cssDefault);
HBox root = new HBox();
root.setSpacing(10);
HBox.setMargin(source, new Insets(10,10,10,10));
HBox.setMargin(target, new Insets(10,10,10,10));
root.getChildren().addAll( source, target);
Scene scene = new Scene(root, 600, 330, Color.WHITE);
primaryStage.setScene(scene);
primaryStage.show();
source.setOnDragDetected(new EventHandler <MouseEvent>() {
public void handle(MouseEvent event) {
/* allow any transfer mode */
Dragboard db = source.startDragAndDrop(TransferMode.ANY);
/* put a string on dragboard */
ClipboardContent content = new ClipboardContent();
content.putString(source.getText());
db.setContent(content);
event.consume();
}
});
target.setOnDragOver(new EventHandler <DragEvent>() {
public void handle(DragEvent event) {
/* accept it only if it is not dragged from the same node
* and if it has a string data */
if (event.getGestureSource() != target &&
event.getDragboard().hasString()) {
/* allow for both copying and moving, whatever user chooses */
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
// position caret at drag coordinates
TextAreaSkin skin = (TextAreaSkin) target.getSkin();
int insertionPoint = skin.getInsertionPoint(event.getX(), event.getY());
target.positionCaret( insertionPoint);
}
event.consume();
}
});
target.setOnDragDropped(new EventHandler <DragEvent>() {
public void handle(DragEvent event) {
/* if there is a string data on dragboard, read it and use it */
Dragboard db = event.getDragboard();
boolean success = false;
if (db.hasString()) {
target.insertText( target.getCaretPosition(), db.getString());
success = true;
}
/* let the source know whether the string was successfully
* transferred and used */
event.setDropCompleted(success);
event.consume();
}
});
}
}
import com.sun.javafx.scene.control.skin.TextAreaSkin;
import com.sun.javafx.scene.text.HitInfo;
/****************************************************/
TextAreaSkin skin = (TextAreaSkin) target.getSkin();
HitInfo mouseHit = skin.getIndex(e.getX(), e.getY());
// Now you can position caret
skin.positionCaret(mouseHit, false, false);
// And/or get insertion index
int insertionPoint = mouseHit.getInsertionIndex();
对于TextArea
这个方法等同于罗兰的回答。这种方法的实际区别在于它适用于TextField
(TextInputControl
的另一个子class):
TextFieldSkin skin = (TextFieldSkin) target.getSkin();
HitInfo mouseHit = skin.getIndex(e.getX(), e.getY());
skin.positionCaret(mouseHit, false);
int insertionPoint = mouseHit.getInsertionIndex();
不幸的是,TextFieldSkin
不会覆盖 getInsertionPoint(...)
和父级的实现 returns 0,因此替代解决方案在这里不起作用。
关于Java9,罗兰和我的回答仍然有效。
软件包 com.sun.javafx.scene.control.skin
和 com.sun.javafx.scene.text
(HitInfo
class 所在的位置)正在移至 Java 9 中的 public API。它们的位置分别是 javafx.scene.control.skin
和 javafx.scene.text
。在此处查看 JavaFX 9 的 Java 文档:http://download.java.net/java/jdk9/jfxdocs/index.html
要始终看到光标,请将其放入 setOnDragOver
-方法
target.requestFocus();