滚动javafx后保持元素X位置固定

时间:2016-03-26 23:02:06

标签: javafx nodes scrollpane

如果在scrollPane中滚动后,如何保持元素始终可见,这意味着在水平滚动之后节点应该是不可移动的。应该修复位置。我已经尝试了this但是它对我的情况不起作用,元素仍在滚动,我正在添加一个包含AnchorPane所有元素的scrollPane。

1 个答案:

答案 0 :(得分:0)

只需使用堆栈窗格并在ScrollPane之后添加固定元素。 根据您不希望允许的滚动,只需注释掉" scrollProperty"我添加的监听器 - 如果你想完全修复元素 - 将它们都删除:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {

        StackPane root = new StackPane();
        root.setAlignment(Pos.TOP_LEFT);

        ScrollPane scrollPane = new ScrollPane();
        Pane pane = new Pane();
        pane.setMinHeight(1000);
        pane.setMinWidth(1000);
        scrollPane.setContent(pane);

        root.getChildren().add(scrollPane);
        Label fixed = new Label("Fixed");
        root.getChildren().add(fixed);

        // Allow vertical scrolling of fixed element:
        scrollPane.hvalueProperty().addListener( (observable, oldValue, newValue) -> {
            double xTranslate = newValue.doubleValue() * (scrollPane.getViewportBounds().getWidth() - fixed.getWidth());
            fixed.translateXProperty().setValue(-xTranslate);
        });
        // Allow horizontal scrolling of fixed element:
        scrollPane.vvalueProperty().addListener( (observable, oldValue, newValue) -> {
            double yTranslate = newValue.doubleValue() * (scrollPane.getViewportBounds().getHeight() - fixed.getWidth());
            fixed.translateYProperty().setValue(-yTranslate);
        });

        Scene scene = new Scene(root, 500, 500);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
相关问题