2016-07-04 37 views
1

我正在为我的大学项目工作的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的问题。

回答

0

您正在设置fixedCellSize="38.0",在HORIZONTAL方向的情况下方向是单元格的宽度。所以你应该把它设置为期望的值(从你的例子我猜它是27.5)。 你也可能想,无论是使用setPadding在你的ListCell实现手动设置它摆脱电池的填充(新插图(0))或CSS:

.list-cell { 
    -fx-padding: 0; 
} 
相关问题