Caret position relative to screen coordinates in javaFX

melkhaldi

I am trying to find a way to get the corresponding screen location of the caret-position in a text area in JavaFX. I need the location to show Popups in text at the caret location.

I found request or it here: https://bugs.openjdk.java.net/browse/JDK-8090849

and some workarounds here: https://community.oracle.com/thread/2534556

They work somehow, but there are a few issues with location not updating correctly sometimes. Does anyone have a suggestion of how to get the caret position in terms of screen X and Y?

Nadeem Douba

Just wanted to follow-up with an answer to this question for TextField controls in JavaFX. I'm sure the same could apply to other text input controls as well. I got the idea from looking at some code that involved changing the default colour of the caret using a subclass of TextFieldSkin class. If you look closely, the TextFieldSkin superclass holds a reference to the Path instance which represents the caret in a protected field called caretPath. Although this is kind of a hack'ish solution, it does provide developers with the absolute coordinates of the Caret in a much safer way than most of the hacks I've seen out there.

public class TextFieldCaretControlSkin extends TextFieldSkin {
    public TextFieldCaretControlSkin(TextField textField, Stage stage) {
        super(textField);
        Popup popup = new Popup();

        // Make the popup appear to the right of the caret
        popup.setAnchorLocation(PopupWindow.AnchorLocation.CONTENT_BOTTOM_LEFT);
        // Make sure its position gets corrected to stay on screen if we go out of screen
        popup.setAutoFix(true);
        // Add list view (mock autocomplete popup)
        popup.getContent().add(new ListView<String>());

        // listen for changes in the layout bounds of the caret path
        caretPath.layoutBoundsProperty().addListener(new ChangeListener<Bounds>() {
            @Override
            public void changed(ObservableValue<? extends Bounds> observable, 
                                Bounds oldValue, Bounds newValue) {
                popup.hide();
                // get the caret's x position relative to the textfield.
                double x = newValue.getMaxX(); 
                // get the caret's y position relative to the textfield.
                double y = newValue.getMaxY(); 
                Point2D p = caretPath.localToScene(x, y);

                /*
                 * If the coordinates are negatives then the Path is being
                 * redrawn and we should just skip further processing.
                 */
                if (x == -1.0 || y == -1.0)
                    return;

                // show the popup at these absolute coordinates.                    
                popup.show(textField,
                        p.getX() + caretPath.getScene().getX() + 
                             caretPath.getScene().getWindow().getX(),
                        p.getY() + caretPath.getScene().getY() + 
                             caretPath.getScene().getWindow().getY() - 
                             newValue.getHeight()); // put the popup on top of the caret
            }
        });
    }
}

To use you'd have to embed this in some sort of subclassed text input control and remember to do textField.setSkin(new TextFieldCaretControlSkin(textField)). There may be better ways to do this since I am not a JavaFX expert but I just wanted to share this solution with the rest of the world just in case it provided some insight.

Hope this helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to position Views on an Android screen relative to the screen edges using offsets in terms of pixel position on the screen coordinates?

From Dev

GetWindowRect coordinates not screen-relative

From Dev

Mouse Coordinates To Caret Position in Firemonkey TMemo Component

From Dev

How To Find INPUT And TEXTAREA Caret Position Coordinates Using JavaScript?

From Dev

Get screen coordinates of a node in javaFX 8

From Dev

Translating floor plan position into coordinates on screen map

From Dev

Get position of ImageView relative to screen programmatically

From Dev

touch position relative to the screen. windows phone

From Dev

Position four relative layouts equally on screen

From Dev

touch position relative to the screen. windows phone

From Dev

Position four relative layouts equally on screen

From Dev

How can I get a component's position on screen (screen coordinates)

From Dev

Wrong coordinates white getting real position of item relative to its parent

From Dev

How to position a relative element in the same coordinates as his parent in the document?

From Dev

relative/absolute position css (convert area element rect coordinates)

From Dev

How to position a relative element in the same coordinates as his parent in the document?

From Dev

JavaFx 8 - Scaling / zooming ScrollPane relative to mouse position

From Dev

Adobe Flash Actionscript Augmented Reality object position relative to screen

From Dev

A CSS (or JavaScript) way of making an elements position relative to the screen resolution

From Dev

Find position of mouse relative to control, rather than screen

From Dev

CSS: How to get position and size relative to screen width?

From Dev

Make a DIV full screen and act like position relative

From Dev

Bad caret position message

From Dev

Bad caret position message

From Dev

How do I get my mouse coordinates relative to the window rather than the screen?

From Dev

F#, Extracting grid coordinates from mouse cursor position relative to an IInputElement

From Dev

Relative coordinates in DrawingBrush

From Dev

Relative coordinates in DrawingBrush

From Dev

Transformation the screen coordinates to stage coordinates

Related Related

  1. 1

    how to position Views on an Android screen relative to the screen edges using offsets in terms of pixel position on the screen coordinates?

  2. 2

    GetWindowRect coordinates not screen-relative

  3. 3

    Mouse Coordinates To Caret Position in Firemonkey TMemo Component

  4. 4

    How To Find INPUT And TEXTAREA Caret Position Coordinates Using JavaScript?

  5. 5

    Get screen coordinates of a node in javaFX 8

  6. 6

    Translating floor plan position into coordinates on screen map

  7. 7

    Get position of ImageView relative to screen programmatically

  8. 8

    touch position relative to the screen. windows phone

  9. 9

    Position four relative layouts equally on screen

  10. 10

    touch position relative to the screen. windows phone

  11. 11

    Position four relative layouts equally on screen

  12. 12

    How can I get a component's position on screen (screen coordinates)

  13. 13

    Wrong coordinates white getting real position of item relative to its parent

  14. 14

    How to position a relative element in the same coordinates as his parent in the document?

  15. 15

    relative/absolute position css (convert area element rect coordinates)

  16. 16

    How to position a relative element in the same coordinates as his parent in the document?

  17. 17

    JavaFx 8 - Scaling / zooming ScrollPane relative to mouse position

  18. 18

    Adobe Flash Actionscript Augmented Reality object position relative to screen

  19. 19

    A CSS (or JavaScript) way of making an elements position relative to the screen resolution

  20. 20

    Find position of mouse relative to control, rather than screen

  21. 21

    CSS: How to get position and size relative to screen width?

  22. 22

    Make a DIV full screen and act like position relative

  23. 23

    Bad caret position message

  24. 24

    Bad caret position message

  25. 25

    How do I get my mouse coordinates relative to the window rather than the screen?

  26. 26

    F#, Extracting grid coordinates from mouse cursor position relative to an IInputElement

  27. 27

    Relative coordinates in DrawingBrush

  28. 28

    Relative coordinates in DrawingBrush

  29. 29

    Transformation the screen coordinates to stage coordinates

HotTag

Archive