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 전문가가 아니기 때문에 더 나은 방법이있을 수 있습니다.하지만이 솔루션을 일부 통찰력을 제공하는 경우를 대비해 다른 사람들과 공유하고 싶었습니다.
희망이 도움이됩니다.
감사합니다. Tomas! 이것은 아주 청초한 해결책입니다! – melkhaldi