How do I get the screen caret coordinates in TextArea?

Hello

I use a component TextArea and when you write a few words, I want to display a popup with suggestions when the user presses CTRL + SPACE. However, I have not found anything that could give me the caret screen coordinates.

So, how can I find the contact details of caret (I intend to use it in a button event handler), or if it is not possible at the moment, what solution do I have for my scenario? (Please note that I want to show the popup to the location of the caret and not anywhere on the screen, because it would be odd to show it to the center of the text example box).

Kind regards
subzero

Published by: subzero on May 5, 2013 15:19

Published by: subzero on May 5, 2013 15:19

I don't see a reasonable way to do it. You can put a feature request in JIRA.

I have a totally unreasonable, though. The circumflex accent is represented as a path (at least, it is in the current versions of JavaFX I use; 2.2.7 (JDK 1.7.0_21) and 8.0.0 (JDK 1.8.0 ea b87)); and there seems to be no other path in the graphics scene below a TextArea. Then you can simply search through the graphic scene, starting from the text box, until you find a path and then get its coordinates to the screen. It's really a hack; It is extremely vulnerable to changes in the implementation of the way a TextArea component is rendered. The carat has no style class or id is defined, so using textArea.lookup (...) doesn't seem to help.

Maybe someone can see better?

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Path;
import javafx.stage.Popup;
import javafx.stage.Stage;
import javafx.stage.Window;

public class CaretCoordinatesTest extends Application {

  @Override
  public void start(Stage primaryStage) {
    final TextArea textArea = new TextArea();
    final BorderPane root = new BorderPane();
    root.setCenter(textArea);

    final Popup popup = new Popup();
    popup.setAutoHide(true);
    VBox suggestionBox = new VBox();
    suggestionBox.setStyle("-fx-border-color: -fx-accent;");
    popup.getContent().add(suggestionBox);
    suggestionBox.getChildren().add(new Label("Here are some suggestions:"));
    for (int i = 1; i <= 10; i++) {
      suggestionBox.getChildren().add(new Label("Suggestion " + i));
    }

    textArea.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler() {
      @Override
      public void handle(KeyEvent event) {
        if (event.getCode() == KeyCode.SPACE && event.isControlDown()) {
          Path caret = findCaret(textArea);
          Point2D screenLoc = findScreenLocation(caret);
          popup.show(textArea, screenLoc.getX(), screenLoc.getY() + 20);
        }
      }
    });

    textArea.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler() {
      @Override
      public void handle(KeyEvent event) {
        if (popup.isShowing()) {
          popup.hide();
        }
      }
    });

    primaryStage.setScene(new Scene(root, 600, 400));
    primaryStage.show();
  }

  private Path findCaret(Parent parent) {
    // Warning: this is an ENORMOUS HACK
    for (Node n : parent.getChildrenUnmodifiable()) {
      if (n instanceof Path) {
        return (Path) n;
      } else if (n instanceof Parent) {
        Path p = findCaret((Parent) n);
        if (p != null) {
          return p;
        }
      }
    }
    return null;
  }

  private Point2D findScreenLocation(Node node) {
    double x = 0;
    double y = 0;
    for (Node n = node; n != null; n=n.getParent()) {
      Bounds parentBounds = n.getBoundsInParent();
      x += parentBounds.getMinX();
      y += parentBounds.getMinY();
    }
    Scene scene = node.getScene();
    x += scene.getX();
    y += scene.getY();
    Window window = scene.getWindow();
    x += window.getX();
    y += window.getY();
    Point2D screenLoc = new Point2D(x, y);
    return screenLoc;
  }

  public static void main(String[] args) {
    launch(args);
  }
}

Edited by: James_D 5 may 2013 18:04

Tags: Java

Similar Questions

  • How can I get the screen to stop on the iPad 2 air?

    How can I get the iPad Air 2 to stop turning the screen when I turn the iPad? Thanks for any help!

    Swipe up from the bottom of the screen to bring up the control center. Then press the icon that looks like a padlock inside a circular arrow. It's the lock of course button.

  • My screen flipped upside down, so, how do I get the screen to lift the right side?

    Somehow, I've changed my screen is upside down, and I don't know how to restore it.  I looked at the Control Panel, but it's too hard trying to read everything backwards.  How do I change so the screen is right side up.  I restarted the computer, but that didn't help.  Suggestions? And thanks in advance for any help I get.

    Thank you very much.  The first option worked!  Thanks for your help.

    You are welcome.

  • How can I get the screen foxfire of origin at the time I opened foxfire?

    Now, I get an advertisement below the place where I enter my question. I had to remove it and return to the original screen life

    Hello

    Try Firefox Safe mode to see if the problem goes away. Safe mode is a troubleshooting mode, which disables most of the modules.

    (If you use it, switch to the default theme).

    • Under Windows, you can open Firefox 4.0 + in Safe Mode holding the key SHIFT key when you open the desktop Firefox or shortcut in the start menu.
    • On Mac, you can open Firefox 4.0 + in Safe Mode holding the key option key when starting Firefox.
    • Under Linux, you can open Firefox 4.0 + with leaving Firefox then go to your Terminal and running Safe Mode: firefox-safe-mode (you may need to specify the installation path of Firefox for example/usr/lib/firefox)
    • Or open the Help menu and click on the restart with the disabled... modules menu item while Firefox is running.

    Once you get the pop-up, simply select "" boot mode safe. "

    If the issue is not present in Firefox Safe Mode, your problem is probably caused by an extension, and you need to understand that one. To do this, please follow article Troubleshooting extensions, themes and problems of hardware acceleration to resolve common Firefox problems .

    To exit safe mode of Firefox, simply close Firefox and wait a few seconds before you open Firefox for normal use again.

    When find you what is causing your problems, please let us know. It might help others who have the same problem.

    Thank you.

  • How can I get the screen keyboard to stop appearing?

    whenever I sign on my user account from the keyboard to the screen, but also a pop magnifer up how can I prevent this?

    http://ITsVISTA.com/2007/06/ITsVISTA-tip-60-hide-a-persistant-on-screen-keyboard-in-Vista/

    How to disable the on-screen keyboard.

    http://wiki.answers.com/Q/How_do_you_turn_off_magnifier_in_windows_vista

    Click on the windows at the bottom of the icon to the left of your screen. Go to the control panel. Then click on ease of access. Now click ease of access Center. Now, click on make the computer easier to see. Now, uncheck activate the narrator and/or turn on the Magnifier. To finish, click on apply.

    See you soon. Mick Murphy - Microsoft partner

  • How do I get the screen to normal size? How to get the start back to normal size icon?

    Vista Home premium pc / sleep monitor mode zooms in to reduce the size of the screen area work / the start icon and the menus are huge / affecting the visio program for now because I didn't need to check other files

    Hello

    Right click on desktop - customize - display settings - Set resolution.

    Right click on desktop - customize - display settings - top left adjust font size (DPI)

    Your mouse/touchpad probably incorporates a ZOOM to it.

    Hold down the CTRL key and move your central wheel of the mouse.

    I hope this helps.
    Rob - bicycle - Mark Twain said it is good.

  • How do I get the screen savers the previous operating system running

    Hello

    I would like to be able to run some old screensavers that come from Windows 98. Of all the screensavers Win98, some of them do not work, but others who have separate .dll files do not work in my new OS. I tried to copy their files .scr so their .dll files in C:\Windows\System32, but when I try to run I get a blank screen.

    I would appriciate help to be able to run them, or otherwise please could you let me know how to get a compatible version of these same screensavers. Screensavers are as follows:

    Screensavers Win98:

    Baseball. SCR
    dangerous creatures.SCR
    inside your computer.scr
    jungle. SCR
    Leonardo da vinci.scr
    Mystery.SCR
    nature. SCR
    Space.SCR
    Sports.SCR
    the era.scr of gold
    Travel.SCR
    Underwater.SCR

    I would appreciate your help.

    Kind regards

    Ray333

    I'm sorry, but you can't do it natively. Also be extremely careful on getting questionable sites such as Freeze.com and Screensavers.com screen savers that will install malicious software on your computer.

    If you decide to try a third party software to do this, it would be wise to image on your computer first with something like Acronis True Image, so if you break Vista irrevocably you can quickly return to a system that works.

    MS - MVP - Elephant Boy computers - don't panic!

  • How do you get the screen message appears only one for each case?

    Hi friends,

    I'm here with another question...

    I built a VI that must contain 6 boxes, when a particular check box is selected by the user, it should display a message to the user that the checkbox is enabled.

    My problem is: when I select one of the boxes the on-screen message continuously one after the other, even when you press the OK button.

    Can someone help me please?

    An option button allows only a boolean in it to be active at a time.  You see these things all the time.  Think about a survey that only lets you select one option.

  • How do I get the screen width and size of the screen of the Blackberry storm?

    Hi guys,.

    I use the following code for the image capture.

    Bitmap bitmap = new Bitmap (Display.getWidth (), Display.getHeight ());

    try {}

    Display.screenshot (aBitmap);

    }

    In 4.3.0 (8120 device), when we use Display.getWidth () and Display.getHeight () it gives 240 x 320 respectively which has the correct size of the device.

    But in 4.7.0 Blackberry storm, when we use it gives data(240x320) even where the size of the device screen real is 360 x 480.

    So when we capture the image in the storm he's only half of the image. And also when hard code us the image like it from 360 x 480 size is thrown exception.

    Please suggest is there a way to capture full screen.

    Thank you

    Thanks for sharing the result. You can mark the thread as solved.

  • How can I get the screen adapters?

    I just got this old Tower after moving... load the windows xp disc with what he came... seems that it does not load the graphics cards... There is no subject in the Device Manager... it only picks up the card while it is plugged... I have been through the system and can not find anything to do with the device...
    Help...
    thnx

    The rocks of XP has track you on the right.

    But there are a few weird things...

    Information on the system that you are using an Intel 82810 Graphics adapter... but as near as I can tell, the motherboard for Dell XPS Gen 2 is not any graphical support integrated:
    http://www.lyberty.com/tech/sysspecs/XPS/ and http://ark.intel.com/products/27711/Intel-82875P-Memory-Controller

    You say that your graphics card is a Radeon 4670, but according to Dell, the XPS Gen 2 came with a Radeon 9800 Pro, a Radeon 9800 XT or an nVidia GeForce 5200.

    Your old tower again, you may have been improved.  If you really have a Radeon 4670 AND it is in the agp slot (there's a pci-e version of the Radeon 4670), you can download a driver here--> http://support.amd.com/us/kbarticles/Pages/CatalystAGPHotfix.aspx

    And I don't really understand your list of 'components removed,' which is a combination of hardware and software.  It's one thing to delete "Internet games" or "music samples", but I do not understand what is meant by removing Ethernet (LAN), Modems, graphics cards, etc. (and I would be very surprised if a tower as the XPS had same "Wireless Ethernet (WLAN)" in the first place.

  • Display shows upside down, how do I get the screen showing the right direction please?

    *******

    Hello

    There are a lot of discussions in these Forums with this information to solve your problem:

    Press Ctrl + Alt + arrow keys OR if it does not, try the other arrows.

    Or right click on the empty Desptop > graphics > Rotation.

    See you soon.

  • Just recently, my screen is black and the words are white or a color, only when I open firefox? How can I get the screen back to white?

    bold text

    See:

  • How do you get the list of bookmarks affixed permanently on the left side of the screen?

    I managed to get the bookmarks permanently open and displayed in a vertical column on the left side of the screen. I accidentally closed and cannot get it to appear again. How do you get the list of bookmarks permanently opened and displayed again? Thank you.

    You can enable or disable the power bookmarks bar by ' display > lateral bars > bookmarks "or Ctrl + B

    You can enable or disable the sidebar story power by ' display > lateral bars > History "or Ctrl + H

    Press the F10 key or press the Alt key to bring up the 'Menu Bar' temporarily if the menu bar is hidden.

  • Satellite Pro U400 - How can I get the native screen resolution for external monitor?

    I have a samsung syncmaster external screen connected to a Satellite Pro U400.

    The resolution of the screen goes up to 1280 etc. but the minimum samsung is 1440 x 920 (ish I forgot).
    How can I get the samsung to display full screen expanded to save my eyes of the realativively small screen internal?

    Hello!

    Have you tried to update the display on your laptop driver? Check the Toshiba site for an update:
    http://EU.computers.Toshiba-Europe.com-online decision-making supported Downloads & => Download drivers

    In addition, it would be interesting to know if you are running the factory settings or you have installed your own version of Windows?
    And what operating system are you using? Have you installed all Windows updates?

    For external LCD, monitors, you must set the screen refresh rate of 60 Hz.
    And if you want to change the resolution of the screen, you must select the second monitor in the graphics options on Windows.

    Good bye

  • All of a sudden my shrunken screen size... How can I get the display back to what it was

    All of a sudden my shrunken screen size... How can I get the display back to what it was

    Hello

    Please try this first:

    Hold down the CTRL key, and then press on + (plus) sign several times.

    I hope this helps.

Maybe you are looking for