Sim*_*oon 3 java javafx scrollbar javafx-8
我有一个ScrollPane
包含TilePane
显示图像的 a 。每当我删除其中一张图像时,它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);
}
}
Run Code Online (Sandbox Code Playgroud)
ScrollPane
确保焦点Node
显示在视口内。被单击的AButton
具有焦点。这意味着焦点节点被删除,JavaFX 尝试将焦点移动到内容中的另一个节点ScrollPane
,在本例中是第一个节点Button
。ScrollPane
将此节点滚动到视图中,这解释了观察到的行为。
解决方案是将everyfocusTraversable
的属性设置为。Button
false
仍然允许您通过更改焦点的另一个选项Tab是重写 的onTraverse
方法ScrollPaneSkin
,因为这是负责此行为的方法:
scroller.setSkin(new ScrollPaneSkin(scroller) {
@Override
public void onTraverse(Node n, Bounds b) {
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1166 次 |
最近记录: |