将组件移出ScrollPane - JavaFX

3
我希望能够为您提供帮助,因为我在JavaFX中的ScrollPane遇到了问题。
我将一个Group添加到ScrollPane中,将该ScrollPane添加到Scene中,并最终将其放入JFXPanel中,因为我在Swing应用程序中使用它并且效果很好。接下来,我向该Group添加了组件(通过组件,我指的是诸如矩形等节点的装配)。这很好,但是当我想将组件移出ScrollPane布局时,组件的移动就变得不可控,它会开始快速移动,而我不知道原因。只有当组件的边框触碰到ScrollPane布局的边缘时,情况才开始变得难以处理。我尝试使用ScrollBar代替但似乎ScrollPane是最适合此功能的对象。
以下是我用于创建视图的代码:
public void createView() {
    this.architectureJFXPanel = new JFXPanel();
    this.group = new Group();
    this.scrollPane = new ScrollPane();


    this.scrollPane.setContent(group);
    this.scrollPane.setOnKeyPressed(new KeyPressed());

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            Scene scene = new Scene(scrollPane, Color.WHITE);
            architectureJFXPanel.setScene(scene);
        }
   });
}

为了将组件放入视图中,我使用group.getChildren.add(),然后将JFXPanel放入swing应用程序中。
以下是我尝试做的示例。您可以看到图片中我试图将组件从布局中移出:

enter image description here

因此,如果有人已经遇到过这种问题并能够帮助我,我将不胜感激 :) 如果需要更多信息以了解问题所在,请向我提问。

我遇到了这个问题,然后使用ScrollBar作为解决方法。 - Thinhbk
所以你必须使用监听器来正确地重新定位场景上的滚动条? - AwaX
1
是的,当然可以。如果你需要的话,我会贴出我的代码。 - Thinhbk
是的,这将很不错 :) 谢谢! - AwaX
我已经发布了我的代码,请让我知道它是否有效。 - Thinhbk
去年我很忙,但今天下午我会尝试,如果成功了我会告诉你的 ;) - AwaX
1个回答

1
这是我使用ScrollBar解决ScrollPane的方法。
public void setStyle() {
    // In my program, TextField controls were generate at run-time, so the prefer height is calculated based on number of controls       
    int preHeight = 1000;
    Group root = new Group();

    // This anchor pane contains all control.
    AnchorPane anchorPane = new AnchorPane();
    // TODO: Add control into this anchor pane
    // anchorPane.getChildren().addAll();

    final ScrollBar sc = new ScrollBar();
    root.getChildren().addAll(anchorPane, sc);

    // Set default size for scene to 800 x 600
    int sceneW = 800;
    int sceneH = 600;
    final Scene scene = new Scene(root, sceneW, sceneH);

    // Setting for the scroll bar
    sc.setLayoutX(scene.getWidth()- sc.getWidth()/2 - 1);
    sc.setMin(0);
    sc.setOrientation(Orientation.VERTICAL);
    sc.setPrefHeight(sceneH);
    sc.setMax(preHeight - sceneH);

    // In case prefer height is smaller than scene height then hide the scroll bar
    if(preHeight < sceneH) {
        sc.setVisible(false);
    }

    scene.setRoot(root);

    // Change the scroll bar and the anchor pane when user change scene's width
    scene.widthProperty().addListener(new ChangeListener<Number>() {

        @Override
        public void changed(ObservableValue<? extends Number> arg0,
                Number arg1, Number arg2) {
            sc.setLayoutX(arg2.doubleValue()- sc.getWidth()/2 - 1);
            anchorPane.setPrefWidth(arg2.doubleValue() - sc.getWidth());
        }
    });

    // Change the scroll bar and the anchor pane when user change scene's height
    scene.heightProperty().addListener(new ChangeListener<Number>() {

        @Override
        public void changed(ObservableValue<? extends Number> arg0,
                Number arg1, Number arg2) {
            sc.setMax(preHeight - arg2.doubleValue());
            if(arg2.doubleValue() >= preHeight) {
                sc.setVisible(false);
            }
            else {
                sc.setVisible(true);
            }
            sc.setPrefHeight(arg2.doubleValue());
            anchorPane.setPrefHeight(arg2.doubleValue());
        }
    });

    // Change the Y position of anchor pane when user scroll the scroll bar
    sc.valueProperty().addListener(new ChangeListener<Number>() {
        public void changed(ObservableValue<? extends Number> ov,
                Number old_val, Number new_val) {
            anchorPane.setLayoutY(-new_val.doubleValue());
        }
    });

    anchorPane.setPrefSize(scene.getWidth() - sc.getWidth(), scene.getHeight());
}   

这种方法存在一个问题,如果用户使用Tab键在屏幕上导航控件,则滚动条的Y位置不会相应地改变。您可以通过处理锚点面板中可聚焦控件的焦点事件,并相应地设置滚动条的Y位置来解决此问题。


年,它正在工作!:D 现在我正在尝试添加一个scrollListener。但是问题是,当我想将组件移出布局时,滚动条不会移动,你遇到过这个问题吗? - AwaX
1
对于scrollListener,这是好的:this.scene.setOnScroll(new EventHandler() { public void handle(ScrollEvent t) { vBar.setValue(vBar.getValue() - t.getDeltaY()); } }); - AwaX
嗯,我也遇到了同样的问题。我想你可以处理控件的焦点事件,并将滚动条导航到所需位置(你需要计算位置)。 - Thinhbk
这里的vBar是什么,请解释一下。 - adesh singh
我曾经也遇到过这个问题。我的解决方法是:假设您在滚动窗格中添加了一些内容到 AnchorPane 中。我将这个 AnchorPane 的最小尺寸绑定到滚动条的宽度和高度属性上。然后在 Java FX Scene Builder 中,我将其最大高度和宽度设置为“Max”。这样,事情就会根据需要更新。 - melkhaldi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接