How to make textflow selectable
Asked Answered
C

2

8

I have made a TextFlow, as I need to use multiple font-postures (I have set particular "Text" to either italic or normal). Once I display the TextFlow, it's not selectable/copyable. All I need is the TextFlow to be selectable once it's displayed in a scene, so it can be copy/paste-able.

Text example with font-posture (only one for examples sake):

Text volumeText = new Text(volume.getText());
volumeText.setFill(Color.WHITE); 
volumeText.setFont(Font.font("arial", FontPosture.ITALIC, 13));
TextFlow reference = new TextFlow(
                         lastNameText, miscelanous1, firstNameText, miscelanous2);
reference.setLayoutX(115);
reference.setLayoutY(480);
reference.setMaxWidth(500);
control.getChildren().add(reference);
Courtyard answered 22/10, 2015 at 6:42 Comment(0)
C
8

Text and TextFlow in JavaFX are not "selectable".

There is an issue open for this : Text should have API for selecting group of characters based on their position similar to the DOM's Range.

Until the issue is taken care of, your best option is to use a 3rd party control like RichTextFX.

Catchascatchcan answered 22/10, 2015 at 8:54 Comment(3)
An alternate approach might also be to use a WebView rather than a TextFlow to represent the styled Text. The text contents of a WebView can be selected and copy and paste works for it.Helper
Could you take a look at comment to this bug : bugs.openjdk.java.net/browse/JDK-8093029 As I understand the author says that there are some possibilities, maybe using impl classes.Variolous
I did some experiments. WebView appears not to render some special characters like emoji, even many characters which work fine in TextFlow, but is also very heavy, which might be a problem if you're trying to use it as a list cell (which I am). RichTextFX has a different issue - it adds a scroll pane around itself and makes it very difficult to access the required information to determine the actual height so that you can set the height of the view itself correctly. But reading its code, it uses TextFlow to do its actual rendering, so selection must be possible.Rosannrosanna
I
0

I think I got a nasty way of doing the similar job

  1. Text could sense the mouse enter & exit move event

  2. We could set a signal for mouse pressed and release event. Would be nice to have this event triggered on Textflow node

  3. extends Text class to have mouse enter event handler, when the global signal is on and mouse enters text then copy the text to some places and also record the text place in the textflow using sth. like var index = textflow.getChildren().indexof(text) record the pair of index and text

  4. when mouse release or exit event are triggered, sort the texts selected and generate the corresponding texts into clipboard

Innervate answered 28/2, 2022 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.