滚动 Pane 内的 JavaFX 坐标

标签 java javafx scrollbar jscrollpane javafx-8

我在 Vbox 内组织了各种对象,效果非常好。我可以像这样使用layoutX/Y上的getter来访问每个对象的坐标

myObject.getlayoutY() // for example

现在我将 Vbox 放入滚动 Pane 中以允许在 Vbox 上滚动,因此我有以下配置:

<ScrollPane>
<content>
    <VBox>
        <children>
            <myobject1/>
             ....
            <myobjectN/>

        </children>
    </VBox>
</content>

它工作得很好,但这里有一个问题:我不明白它在滚动 Pane 内移动的是什么。

当我尝试在 Vbox 或 myObject 上使用 getlayoutY() 时,即使滚动,我也会得到一个恒定值。

如何轻松访问对象的新坐标,真正实现滚动值?

编辑:我想在滚动 Pane 父级的坐标系中进行转换

注意:我设法访问滚动条值(在 [0-1] 之间),并且可以使用 getLocalToSceneTransform() 访问坐标,但我认为我走错了路。

最佳答案

看看以下示例是否有助于实现您想要的目标:

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.geometry.Bounds;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class BindToScrollingObject extends Application {


    @Override
    public void start(Stage primaryStage) {
        HBox root = new HBox();
        ScrollPane scroller = new ScrollPane();
        VBox vbox = new VBox(20);
        scroller.setContent(vbox);
        Label bindingLabel = new Label("Binding here");
        for (int i = 0; i < 4; i++) {
            vbox.getChildren().add(new Label("Item "+(i+1)));
        }
        vbox.getChildren().add(bindingLabel);
        for (int i = 0; i < 4; i++) {
            vbox.getChildren().add(new Label("Item "+(i+6)));
        }
        Label anchor = new Label("Anchor");
        anchor.setStyle("-fx-background-color: antiquewhite");
        StackPane stack = new StackPane(anchor);

        root.getChildren().addAll(scroller, stack);

        Line line = new Line();
        line.setManaged(false);
        root.getChildren().add(line);

        ChangeListener<Object> listener = (obs, oldValue, newValue) -> 
            updateLine(line, anchor, bindingLabel);

        anchor.boundsInLocalProperty().addListener(listener);
        anchor.localToSceneTransformProperty().addListener(listener);
        bindingLabel.boundsInLocalProperty().addListener(listener);
        bindingLabel.localToSceneTransformProperty().addListener(listener);

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

    private void updateLine(Line line, Node start, Node end) {

        Node target = line.getParent();

        Bounds startBounds = convertBoundsToTarget(start, target);
        Bounds endBounds = convertBoundsToTarget(end, target);


        line.setStartX(startBounds.getMinX() + startBounds.getWidth() / 2);
        line.setStartY(startBounds.getMinY() + startBounds.getHeight() / 2);
        line.setEndX(endBounds.getMinX() + endBounds.getWidth() / 2);
        line.setEndY(endBounds.getMinY() + endBounds.getHeight() / 2);
    }

    private Bounds convertBoundsToTarget(Node node, Node target) {
        Bounds boundsInScene = node.localToScene(node.getBoundsInLocal());
        return target.sceneToLocal(boundsInScene);
    }

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

关于滚动 Pane 内的 JavaFX 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29306715/

相关文章:

JavaFX 桌面 View 滚动条事件未触发

javascript - React .scrollLeft 无法读取空值

java - 围绕 Java 操作应用超时控制

java - 通过URL访问JAR内部文件后可以删除它吗?

java - 无法使用 @Bean 和 @ConfigurationProperties 绑定(bind)属性

java - Spring MVC 与网页设计师合作

intellij-idea - 错误 :(3, 26) java : package javafx. 应用程序不存在

JavaFX 图像 getUrl() 在 Excelsior jet 中导致 NoSuchMethodError

JavaFx 创建表格单元格仅接受数字?

html - 水平滚动条不适用于选择标签