首页 > 解决方案 > 使所有元素(包括 2 个列表视图)在 JavaFX 中一起滚动

问题描述

我有一个 VBox,在 VBox 中有一些元素,包括 3 个标题(3 个堆栈窗格)和 2 个列表视图,是否可以让 VBox 中的所有内容一起滚动?

这是我的应用程序的图像

在此处输入图像描述

这是此视图的 FXML 代码

<VBox xmlns="http://javafx.com/javafx/11" xmlns:fx="http://javafx.com/fxml/1">
   <StackPane prefHeight="30.0" prefWidth="200.0" styleClass="stack-welcome" VBox.vgrow="NEVER">
      <children>

         <!-- WELCOME MESSAGE -->
         <Label styleClass="label-welcome" text="Hi! I'm Volant :)" />
      </children>
   </StackPane>
   <StackPane prefHeight="15.0" prefWidth="200.0" styleClass="stack-header" VBox.vgrow="NEVER">
      <children>
        <Label styleClass="label-header" text="TRIP LIST" />
      </children>
   </StackPane>

   <StackPane prefHeight="15.0" prefWidth="200.0" styleClass="trip-list-header" style="-fx-border-width: 0px 0px 2px 0px" VBox.vgrow="NEVER">
      <children>
         <Label styleClass="label-header" text="UPCOMING TRIPS" />
      </children>
   </StackPane>
   <ListView fx:id="tripListViewUpcoming" prefWidth="247.0" style="-fx-background-color: #fff;" VBox.vgrow="ALWAYS" />

   <StackPane prefHeight="15.0" prefWidth="200.0" styleClass="trip-list-header" style="-fx-border-width: 2px 0px 2px 0px" VBox.vgrow="NEVER">
      <children>
         <Label styleClass="label-header" text="PAST TRIPS" />
      </children>
   </StackPane>
   <ListView fx:id="tripListViewPast" prefWidth="247.0" style="-fx-background-color: #fff;" VBox.vgrow="ALWAYS" />

</VBox>

标签: javajavafx

解决方案


我设法通过用 scrollPane 包装 VBox 并设置来使整个 VBox 一起滚动scrollPane.setFitToWidth(true);


推荐阅读