css - 将滚动面板的垂直滚动条移至左侧

标签 css javafx scrollbar

使用 JavaFX,我想将滚动面板的垂直滚动条移动到组件的左侧,而不是默认的右侧。我尝试在 CSS 中使用 -fx-alignment 来做到这一点,但不起作用。

.scroll-pane .scroll-bar:vertical {
    -fx-alignment: LEFT; }

最佳答案

你可以

  • 创建滚动条
  • 将 ScrollBar 属性绑定(bind)到 ScrollPane 的相关属性
  • 隐藏 ScrollPane 的 ScrollBar

这是一个快速草稿:

ExternalScrollbar.java

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class ExternalScrollbar extends Application {

    @Override
    public void start(Stage stage) {

        Pane pane = new Pane();
        Line line = new Line(100, 100, 1000, 1000);
        pane.getChildren().add(line);

        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(pane);

        ScrollBar vScrollBar = new ScrollBar();
        vScrollBar.setOrientation(Orientation.VERTICAL);
        vScrollBar.minProperty().bind(scrollPane.vminProperty());
        vScrollBar.maxProperty().bind(scrollPane.vmaxProperty());
        vScrollBar.visibleAmountProperty().bind(scrollPane.heightProperty().divide(pane.heightProperty()));
        scrollPane.vvalueProperty().bindBidirectional(vScrollBar.valueProperty());

        ScrollBar hScrollBar = new ScrollBar();
        hScrollBar.setOrientation(Orientation.HORIZONTAL);
        hScrollBar.minProperty().bind(scrollPane.hminProperty());
        hScrollBar.maxProperty().bind(scrollPane.hmaxProperty());
        hScrollBar.visibleAmountProperty().bind(scrollPane.widthProperty().divide(pane.heightProperty()));
        scrollPane.hvalueProperty().bindBidirectional(hScrollBar.valueProperty());

        // hide scrollpane scrollbars
// TODO: re-activate the code       
//      scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
//      scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
//      scrollPane.setPadding(Insets.EMPTY);

        HBox hBox = new HBox();
        HBox.setHgrow(scrollPane, Priority.ALWAYS);
        hBox.getChildren().addAll(vScrollBar, scrollPane);

        VBox vBox = new VBox();
        VBox.setVgrow(hBox, Priority.ALWAYS);
        vBox.getChildren().addAll(hScrollBar, hBox);

        Scene scene = new Scene(vBox, 500, 400);
        scene.getStylesheets().add(this.getClass().getResource("style.css").toExternalForm());

        stage.setScene(scene);
        stage.show();

        vScrollBar.requestLayout();
        hScrollBar.requestLayout();
    }

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

样式.css

.scroll-pane {
    -fx-background-insets: 0;
    -fx-padding: 0;
}

.scroll-pane:focused {
    -fx-background-insets: 0;
}

.scroll-pane .corner {
    -fx-background-insets: 0;
}

当然,您必须激活隐藏 ScrollPane 的 ScrollBar 的代码。

enter image description here

关于css - 将滚动面板的垂直滚动条移至左侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35134155/

相关文章:

javafx - 未选中复选框时如何停止转换javafx

java - 在Eclipse上导出应用程序后的不同字符

html - 为什么第 nth-child 会选择所有这些?

javascript - JQuery:根据浏览器大小调整图像大小

java - 对 -fx-alignment : LEFT saying no enum constant javafx. geometry.Pos.LEFT 发出警告

javascript - IE7模态对话框滚动条重叠内容

iphone - 如何向 iPhone 网站添加滚动条?

jquery - 当可滚动侧边栏打开时禁用主要内容的滚动

css - 关键帧不产生任何动画效果

css - :empty selector not working in css