我有一个ScrollPane
含有一个TilePane
显示图像。每当我删除其中一张图像时ScrollPane
跳回到顶部,这在尝试删除多个图像时非常烦人。有没有办法控制滚动行为?
我在 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
确保重点突出Node
显示在视口内。 AButton
单击的内容具有焦点。这意味着焦点节点被删除,并且 JavaFX 尝试将焦点移动到该节点中的另一个节点。ScrollPane
内容,在本例中是第一个Button
. ScrollPane
将此节点滚动到视图中,这解释了观察到的行为。
一个解决方案是设置focusTraversable
每个人的财产Button
to false
.
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) {
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)