E 'possibile ottenere la posizione del cursore (uno int
) su una coordinata dello schermo (2 double
) in JavaFX TextArea
?JavaFX TextArea ottiene la posizione del cursore dalla coordinata
risposta
È possibile utilizzare il metodo getInsertionPoint del TextAreaSkin nel gestore di trascinamento:
TextAreaSkin skin = (TextAreaSkin) target.getSkin();
int insertionPoint = skin.getInsertionPoint(event.getX(), event.getY());
target.positionCaret(insertionPoint);
Tuttavia, la classe pelle è in com.sun.javafx *, quindi con Java 9 che esce Avrete probabilmente. devono fare le cose diversamente allora. Nessuno sa cosa si romperanno o cosa forniranno in sostituzione. Tuttavia, con Java 8 funziona (per ora).
esempio completo in cui è possibile trascinare il testo di un'etichetta in qualsiasi posizione nella 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();
Per TextArea
questo metodo è equivalente alla risposta di Roland. La differenza pratica di questo metodo è la sua applicabilità ad TextField
(un'altra sottoclasse di TextInputControl
):
TextFieldSkin skin = (TextFieldSkin) target.getSkin();
HitInfo mouseHit = skin.getIndex(e.getX(), e.getY());
skin.positionCaret(mouseHit, false);
int insertionPoint = mouseHit.getInsertionIndex();
Purtroppo, TextFieldSkin
non esclude la getInsertionPoint(...)
e la realizzazione del genitore restituisce 0 in modo che la soluzione alternativa non funziona qui.
Per quanto riguarda Java 9, sia Roland che le mie risposte funzioneranno ancora. I pacchetti com.sun.javafx.scene.control.skin
e com.sun.javafx.scene.text
(dove si trova la classe HitInfo
) si spostano sull'API pubblica in Java 9. Le loro posizioni saranno javafx.scene.control.skin
e javafx.scene.text
, rispettivamente. Vedere Javadocs per JavaFX 9 qui: http://download.java.net/java/jdk9/jfxdocs/index.html
Per vedere sempre il cursore, mettere questo nel setOnDragOver
-Methode
target.requestFocus();
questo non risponde alla domanda –
- 1. aggiornamento valore textarea, ma mantenere la posizione del cursore
- 2. ottiene la posizione del puntatore del terminale
- 3. TinyMCE perde posizione del cursore
- 4. Come si ottiene la posizione del cursore in un'area di testo?
- 5. Ottiene la posizione del mouse
- 6. ottiene la posizione reale di un nodo in javaFX
- 7. jQuery: ottiene la posizione del cursore del testo in input senza codice specifico del browser?
- 8. Imposta la posizione del cursore in un widget di testo
- 9. Come trovare la posizione del cursore in una JTextArea
- 10. Preservare la posizione del cursore con angularjs
- 11. Posso ripristinare la posizione del cursore all'inizio?
- 12. JavaFX Più colori in textarea (JavaFX 8)
- 13. Come impostare la posizione del cursore in GWT RichTextArea
- 14. JavaFX TextArea e autoscroll
- 15. JavaFX TextArea Evento OnChange
- 16. onMouseMove ottiene la posizione del mouse
- 17. Ottiene la posizione del mouse nell'evento tastiera
- 18. Come trovare la posizione globale del cursore del testo?
- 19. Posizione del cursore di EditText
- 20. TextField() Imposta posizione del cursore
- 21. Posizione del cursore nell'abbreviazione VIM
- 22. Ottieni e imposta la posizione del cursore con div contenteditable
- 23. Trovare la posizione del testo del cursore in JTextField
- 24. impedisce la posizione del cursore di "freccia su" durante la reimpostazione della posizione del cursore all'interno della casella di testo
- 25. posizione del cursore del mouse in Javascript?
- 26. Come posso posizionare un menu a discesa nella posizione del cursore all'interno di una textarea?
- 27. jQuery Imposta la posizione del mouse (non la posizione del cursore)
- 28. Come mantenere la posizione del cursore sulla modifica del testo dell'area di testo?
- 29. coordinata shader libGDX e posizione della telecamera
- 30. posizione del cursore rispetto al Application
Potresti descrivere ciò che si sta cercando di raggiungere? Immagino tu sappia che TextArea eredita caretPosition() da TextInputControl. Ecco perché chiedo. – VinceOPS
@VincentG Sì, sono a conoscenza della funzione 'caretPosition()' che restituisce la posizione corrente del cursore. Sto cercando di implementare un drag-and-drop in modo che gli elementi in un 'TreeView' possano essere trascinati in qualsiasi posizione del testo. –