【问题标题】:ListView cells grow bigger than the Node they containListView 单元格比它们包含的节点大
【发布时间】:2016-07-04 19:30:43
【问题描述】:

我正在为我的大学项目开发​​ GUI, 我必须连续显示一系列由颜色定义的元素。 因为它们可以在运行时更改,所以我使用了 ListView。 元素不能超过四个,它们应该占据一定数量的空间,但是当我将节点放在列表的单元格内时,单元格会在节点周围显示一个边框(在这种情况下是一个矩形)使 ListView 超出他的首选尺寸并显示两个滚动条,我不想看到这两个滚动条,因为 List 不应该是可滚动的,而且它真的很小,所以条形几乎覆盖了所有滚动条。

这是我为细胞工厂编写的代码:

private void initializeBalcony(SimpleListProperty<String> balconyProperty, ListView<String> balconyView) {
        balconyView.setBorder(null);
        balconyView.setItems(balconyProperty);
        balconyView.setCellFactory(row -> new ListCell<String>() {
            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                if (item == null || empty)
                    Platform.runLater(() -> this.setGraphic(null));
                else {
                    this.setMaxSize(27.5, 38);
                    Rectangle councillor = new Rectangle();
                    councillor.setWidth(20);
                    councillor.setHeight(35);
                    councillor.setFill(Paint.valueOf(item));
                    Platform.runLater(() -> this.setGraphic(councillor));
                }
            }
        });
    }

加上 ListView 的 FXML 定义:

<ListView fx:id="coastBalcony" fixedCellSize="38.0" layoutX="80.0" orientation="HORIZONTAL" prefHeight="40.0" prefWidth="120.0" />

Graphical display of the problem

我不知道我做错了什么,也许我应该只使用和HBox,但我曾经遇到过ListChangeListeners的问题。

【问题讨论】:

    标签: java listview user-interface javafx


    【解决方案1】:

    您正在设置fixedCellSize="38.0",在HORIZONTAL 的情况下,方向是单元格的宽度。因此,您应该将其设置为所需的值(从您的示例中我猜它是 27.5)。 此外,您可能希望摆脱单元格填充,方法是在 ListCell 实现中使用 setPadding(new Insets(0)) 或在 css 中手动设置:

    .list-cell {
        -fx-padding: 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2010-11-27
      • 1970-01-01
      • 2023-03-12
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      相关资源
      最近更新 更多