kotlin - JavaFX TreeView 的 TreeCell 全宽
问题描述
嗨stackoverflow社区,
我用 javafx 在 kotlin 中开发了一个应用程序。为此,我需要一个带有自定义 TreeCell 表示的 TreeView。我的问题是始终保留 TreeCell 内容(见下图)。我想要做的是,TreeCell 内容从左到右拉伸。(如 AnchorPane.leftAnchor = 0 和 AnchorPane.rightAnchor = 0)
fxml 看起来像这样:
<VBox prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.bli.bla.blu.conrollers.MyController">
<TreeView fx:id="treeView" showRoot="false" VBox.vgrow="ALWAYS">
</TreeView>
</VBox>
在我调用的控制器中:
treeView?.setCellFactory { PositionTreeCell() }
PositionTreeCell 是:
class PositionTreeCell : TreeCell<PositionEntryController>() {
override fun updateItem(item: PositionEntryController?, empty: Boolean) {
super.updateItem(item, empty)
if (empty || item == null) {
text = null
graphic = null
contextMenu = null
} else {
text = null
graphic = getPositionTreeCellGraphic(item)
contextMenu = null
}
}
private fun getPositionTreeCellGraphic(item: PositionEntryController): Node? {
if (item is TypeA) {
val loader = FXMLLoader()
loader.resources = ResourceBundle.getBundle("Resources", Locale.getDefault())
loader.setControllerFactory {
item
}
val parent = loader.load<Parent>(javaClass.getResourceAsStream("/fxml/typeA.fxml"))
return parent
} else if (item is TypeB)
return TODO()
else
return null
}
}
fxml中的自定义模板是:
<GridPane gridLinesVisible="true" prefHeight="20.0" prefWidth="800" xmlns="http://javafx.com/javafx/10.0.2-internal"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="de.bli.bla.blu.conrollers.PositionDefinitionController">
<Label fx:id="keyLabel"/>
<Label fx:id="positionNameLabel" GridPane.columnIndex="1"/>
<Label fx:id="piecePriceLabel" GridPane.columnIndex="2"/>
<Label fx:id="unitLabel" GridPane.columnIndex="3"/>
<columnConstraints>
<ColumnConstraints maxWidth="100"/>
<ColumnConstraints/>
<ColumnConstraints maxWidth="100"/>
<ColumnConstraints maxWidth="50"/>
</columnConstraints>
<rowConstraints>
<RowConstraints/>
</rowConstraints>
</GridPane>
它看起来像什么:
我尝试的是在 PositionTreeCell 中设置 AnchorPane,但这不起作用。有谁知道如何做到这一点?
解决方案
推荐阅读
- java - DS_Store 文件使 Minecraft forge 崩溃
- html - 使用 3 秒 CSS 过渡来移动屏幕上的元素是否符合 ADA 标准?
- spring-boot - java.lang.NoSuchMethodError: org.mockito.MockingDetails.getMockCreationSettings()Lorg/mockito/mock/MockCreationSettings
- mysql - 使用 CASE SUBSTRING 和 WHERE 从另一个表值更新
- python - 错误代码:附加到 blob 时的 InvalidBlobType
- c# - 如何从指定日期范围的 onvif 设备(IP 摄像机)中检索录制的视频?[C#]
- javascript - 从本地文件读取数据到javascript对象数组
- agora.io - AgoraRTM 查询消息历史(持久消息)
- python - 比较python中的列表值
- ruby-on-rails - 如何在 searchkick 中实现关联