我有一个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
显示在视口内。点击的Button
具有焦点。这意味着删除了焦点节点,JavaFX尝试将焦点移动到ScrollPane
内容中的另一个节点,在这种情况下是第一个Button
。 ScrollPane
将此节点滚动到视图中,这解释了观察到的行为。
一个解决方案是将每个focusTraversable
的Button
属性设置为false
。
另一个仍允许你通过Tab更改焦点的选项将覆盖onTraverse
的ScrollPaneSkin
方法,因为这是导致此行为的方法:
scroller.setSkin(new ScrollPaneSkin(scroller) {
@Override
public void onTraverse(Node n, Bounds b) {
}
});
该问题似乎是由于焦点恢复到磁贴窗格的第一个元素中的按钮引起的。一个解决方法是:
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");
for (int i = 0; i < 20; i++) {
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) {
int index = imagePane.getChildren().indexOf(stackPane);
imagePane.getChildren().remove(index);
int numPanes = imagePane.getChildren().size();
if (numPanes > 0) {
imagePane.getChildren().get(Math.min(index, numPanes-1)).requestFocus();
}
}
});
stackPane.getChildren().addAll(imageView, button);
imagePane.getChildren().add(stackPane);
}
这个答案基于法比安答案。在一些JavaFx imp中,onTraverse
上没有ScrollPaneSkin
方法(参见javafx.scene.control.skin.ScrollPaneSkin vs javafx.scene.control.skin.ScrollPaneSkin)所以它不能是@Override
,所以我的工作方式是一般的解决方案,不包括程序员对每个按钮的更改,但它可以得到根和设置FocusTravers
= false
无论Node
(此imp中的Button
)被添加到视图树。
这个助手类:
public class FocusTraversHelper {
static private ListChangeListener listChangeListener;
static {
listChangeListener=new ListChangeListener<Node>() {
@Override
public void onChanged(Change<? extends Node> c) {
if(c.next()&&c.wasAdded()){
Node b =c.getAddedSubList().get(0);
if(b!=null&&(b instanceof Parent || b instanceof Button) ){
cancelFocusTravers(b);
}
}
}};
}
static private void registerAddEvent(Parent parent){
parent.getChildrenUnmodifiable().removeListener(listChangeListener);
parent.getChildrenUnmodifiable().addListener(listChangeListener);
}
static public void cancelFocusTravers(Node node){
if(node instanceof Parent){
registerAddEvent((Parent)node);
for (Node n: ((Parent) node).getChildrenUnmodifiable()) {
cancelFocusTravers(n);
}
}
if(node instanceof Button){
node.setFocusTraversable(false);
}
}
}
用途如下:
FocusTraversHelper.cancelFocusTravers(root);
希望它会帮助某人
注意:如果有另一个控件,如TextField
,它应该被修改以便工作(也许使用Node
而不是Button
)。
注意:删除时需要删除 - >添加并多次添加同一节点,因此只有一个侦听器