删除节点时,ScrollPane跳转到顶部 [英] ScrollPane jumps to top when deleting nodes

查看:139
本文介绍了删除节点时,ScrollPane跳转到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ScrollPane ,其中包含显示图片的 TilePane 。每当我删除其中一个图像时, ScrollPane 会跳回到顶部,这在尝试删除多个图像时非常烦人。有没有办法控制滚动行为?

I have an ScrollPane containing a TilePane which shows images. Whenever I delete one of those images the ScrollPane jumps back to the top which is quite annoying when trying to remove multiple images. Is there a way to control the scrolling behaviour?

我在Windows 7上运行此代码:

I'm running this code on Windows 7:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;

public class ScrollbarProblem extends Application{
    private TilePane imagePane;
    private ScrollPane scroller;

    @Override
    public void start(Stage primaryStage) {
        imagePane = new TilePane();
        scroller = new ScrollPane();
        scroller.setContent(imagePane);
        BorderPane root = new BorderPane(scroller, null, null, null, null);
        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();

        for(int i = 0; i < 20; i++){
            Image img = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/SIPI_Jelly_Beans_4.1.07.tiff/lossy-page1-220px-SIPI_Jelly_Beans_4.1.07.tiff.jpg");
            final ImageView imageView = new ImageView(img);

            final Button button = new Button("Delete");
            final StackPane stackPane = new StackPane();

            button.setOnAction(new EventHandler<ActionEvent>() {

                @Override
                public void handle(ActionEvent event) {
                    imagePane.getChildren().remove(stackPane);
                }
            });

            stackPane.getChildren().addAll(imageView, button);
            imagePane.getChildren().add(stackPane);
        }
    }

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


推荐答案

ScrollPane 确保在视口内显示焦点节点。单击的按钮具有焦点。这意味着删除了焦点节点,JavaFX尝试将焦点移动到 ScrollPane 内容中的另一个节点,在本例中是第一个 Button ScrollPane 将此节点滚动到视图中,这解释了观察到的行为。

ScrollPane makes sure the focused Node is shown inside the viewport. A Button that is clicked has the focus. This means the focused node is removed and JavaFX tries to move the focus to another node in the ScrollPane content, which in this case is the first Button. ScrollPane scrolls this node into view, which explains the observed behavior.

解决方案是设置 focusTraversable 每个按钮的属性 false

A solution would be setting the focusTraversable property of every Button to false.

另一个仍允许你通过 Tab 更改焦点的选项将覆盖 onTraverse 方法 ScrollPaneSkin ,因为这是导致此行为的方法:

Another option which would still allow you to change the focus via Tab would be overriding the onTraverse method of the ScrollPaneSkin, since this is the method responsible for this behavior:

scroller.setSkin(new ScrollPaneSkin(scroller) {

    @Override
    public void onTraverse(Node n, Bounds b) {
    }

});

这篇关于删除节点时,ScrollPane跳转到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆