2014-04-05 7 views
3

JavaFX의 텍스트 영역에서 캐럿 위치의 해당 화면 위치를 가져 오는 방법을 찾으려고합니다. 캐럿 위치의 텍스트에 팝업을 표시 할 위치가 필요합니다. https://bugs.openjdk.java.net/browse/JDK-8090849javaFX의 화면 좌표를 기준으로하는 캐럿 위치

하고 여기에 몇 가지 해결 방법 :

나는 요청하거나 여기 그들은 어떻게 든 일을 https://community.oracle.com/thread/2534556

을하지만, 위치가 가끔 제대로 업데이트되지와 몇 가지 문제가 있습니다. 누구든지 화면 X와 Y의 관점에서 캐럿 위치를 얻는 방법에 대한 제안을 갖고 있습니까?

답변

2

JavaFX의 TextField 컨트롤에 대한이 질문에 대한 답변을 후속 조치하고 싶습니다. 나는 다른 텍스트 입력 컨트롤에도 똑같이 적용될 수 있다고 확신한다. TextFieldSkin 클래스의 하위 클래스를 사용하여 캐럿의 기본 색상을 변경하는 것과 관련된 코드를 살펴 보았습니다. 자세히 살펴보면 TextFieldSkin 수퍼 클래스는 caretPath이라는 보호 된 필드에서 캐럿을 나타내는 Path 인스턴스에 대한 참조를 보유합니다. 이것은 일종의 해킹 해결책이지만, 개발자가 내가 본 대부분의 해킹보다 훨씬 안전한 방법으로 개발자에게 절대 좌표를 제공합니다.

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 
      } 
     }); 
    } 
} 

는 서브 클래스 텍스트 입력 컨트롤의 일종이를 포함해야하고 textField.setSkin(new TextFieldCaretControlSkin(textField))을 기억 것 사용하십시오. 필자는 JavaFX 전문가가 아니기 때문에 더 나은 방법이있을 수 있습니다.하지만이 솔루션을 일부 통찰력을 제공하는 경우를 대비해 다른 사람들과 공유하고 싶었습니다.

희망이 도움이됩니다.

1

이 당신 캐럿의 오른쪽에 팝업 창 4 픽셀의 위치를 ​​RichTextFX을 사용하는 방법입니다 :

InlineCssTextArea area = new InlineCssTextArea(); 
Popup popup = new Popup(); 
popup.getContent().add(new Label("I am a popup label!")); 
area.setPopupWindow(popup); 
area.setPopupAlignment(PopupAlignment.CARET_CENTER); 
area.setPopupAnchorOffset(new Point2D(4, 0)); 
당신은 여전히 ​​

popup.show(ownerWindow); 
popup.hide(); 
를 호출하여 팝업 창에 자신의 가시성을 제어 할 필요가

참조 : working demo.

+0

감사합니다. Tomas! 이것은 아주 청초한 해결책입니다! – melkhaldi