首页 > 解决方案 > 增加 ListView 宽度以适应内容并删除水平滚动条

问题描述

我想在 ScrollPane 旁边放置一个 ListView。但是当 ListView 的动态内容太宽时,我希望 ListView 变得更宽以适应它的内容。在提供的图像中,您可以看到它如何创建滚动条而不是扩大 ListView。

ListView 中丑陋的水平滚动条

由于我是 JavaFX 新手,我不知道如何实现这种行为。如何使 ListView 变得更宽以自动适应其内容?这可以完全在 FXML 中完成吗?

这是我关注的 UI 部分的 FXML:

<HBox prefHeight="480.0" prefWidth="640.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="28.0">
  <children>
    <ListView fx:id="componentList" prefHeight="-1.0" prefWidth="-1.0" HBox.hgrow="NEVER" />
    <ScrollPane hbarPolicy="ALWAYS" pannable="true" prefHeight="200.0" prefWidth="200.0" vbarPolicy="ALWAYS" HBox.hgrow="ALWAYS">
      <content>
        <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="2000.0" prefWidth="2000.0">
          <children>
            <Rectangle arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="200.0" layoutX="67.0" layoutY="45.0" stroke="BLACK" strokeType="INSIDE" width="200.0" />
          </children>
        </AnchorPane>
      </content>
    </ScrollPane>
  </children>
</HBox>

标签: listviewjavafx

解决方案


推荐阅读