Java ListView单元格的增长大于其包含的节点
我还在为我的大学项目开发GUI, 我必须在一行中显示一系列由颜色定义的元素。 因为它们可以在运行时更改,所以我使用了ListView。 元素不能超过四个,它们应该占据一定的空间,但当我将节点放在列表单元格中时,单元格会在节点周围显示边框(本例中为矩形),这会使ListView超出其首选大小,并显示两个滚动条,这是我不想看到的,因为列表不应该是可滚动的,而且它非常小,所以条形图几乎涵盖了所有内容 以下是我为cell factory编写的代码:Java ListView单元格的增长大于其包含的节点,java,listview,user-interface,javafx,Java,Listview,User Interface,Javafx,我还在为我的大学项目开发GUI, 我必须在一行中显示一系列由颜色定义的元素。 因为它们可以在运行时更改,所以我使用了ListView。 元素不能超过四个,它们应该占据一定的空间,但当我将节点放在列表单元格中时,单元格会在节点周围显示边框(本例中为矩形),这会使ListView超出其首选大小,并显示两个滚动条,这是我不想看到的,因为列表不应该是可滚动的,而且它非常小,所以条形图几乎涵盖了所有内容 以下是我为cell factory编写的代码: private void initializeBalc
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));
}
}
});
}
private void initializeBalcony(SimpleListProperty balconyProperty,ListView balconyView){
balconyView.setboorder(空);
balconyView.setItems(balconyProperty);
balconyView.setCellFactory(行->新建ListCell(){
@凌驾
受保护的void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
如果(项==null | |空)
Platform.runLater(()->this.setGraphic(null));
否则{
这个.setMaxSize(27.5,38);
矩形议员=新矩形();
议员:20人;
议员:塞特高(35);
市议员:setFill(油漆。价值(项目));
Platform.runLater(()->this.setGraphic(counciller));
}
}
});
}
加上ListView的FXML定义:
<ListView fx:id="coastBalcony" fixedCellSize="38.0" layoutX="80.0" orientation="HORIZONTAL" prefHeight="40.0" prefWidth="120.0" />
我不知道我做错了什么,也许我应该只使用和HBox,但我以前在ListChangeListeners上遇到过问题。您正在设置
fixedCellSize=“38.0”
,在水平方向的情况下,它是单元格的宽度。因此,您应该将其设置为所需的值(从您的示例中,我猜是27.5)。
另外,您可能希望通过在ListCell实现中使用setPadding(新插入(0))手动设置单元格填充,或者在css中手动设置:
.list-cell {
-fx-padding: 0;
}
您正在设置fixedCellSize=“38.0”
,在水平方向的情况下,它是单元格的宽度。因此,您应该将其设置为所需的值(从您的示例中,我猜是27.5)。
另外,您可能希望通过在ListCell实现中使用setPadding(新插入(0))手动设置单元格填充,或者在css中手动设置:
.list-cell {
-fx-padding: 0;
}