我正在为我的大学项目工作的GUI, 我必须在一行中显示一系列由它们的颜色定义的元素。 由于他们可以在运行时更改我使用ListView。 不能超过四个元素,它们应该占用一定数量的空间,但是当我将节点放入列表单元格内时,单元格会在节点周围显示一个边框(在这种情况下为矩形)这使得ListView不再显示他的首选大小,并且显示了两个滚动条,这是我不想看到的,因为列表不应该是可滚动的,加上它非常小,所以它们几乎覆盖了所有的滚动条。ListView的单元格比它们包含的节点增长得更大
这是我写的细胞工厂代码:
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的问题。