是否有一种方法可以使用tornadofx中的fxml文件创建ListView的自定义ListCell?
我有一个fxml文件CustomListCell
:
<HBox>
<Label text="File name"/>
<ImageView fitHeight="18.0" fitWidth="18.0" pickOnBounds="true"/>
<stylesheets>
...
</stylesheets>
</HBox>
而且我有CustomListCell.kt
用于此fxml。我想做这样的事情:
val customList = ListView<CustomListCell>()
customList.add(CustomListCell(name, image))
如何为此目的实施CustomListCell.kt
?
我不知道您为什么在这里使用fxml是个好主意,但是通过此示例,您可以得到想要的结果:
class Example: View("Example") {
val listofitem = FXCollections.observableArrayList<TextImagen>()
override val root = vbox {
listofitem.add(TextImagen("Car","car.png"))
listofitem.add(TextImagen("Apple","apple.png"))
listofitem.add(TextImagen("Pencil","pencil.png"))
listview<TextImagen>(listofitem){
cellFormat {
graphic = cache(it){
hbox {
label(it.name)
imageview(it.url){
fitHeight = 18.0
fitWidth = 18.0
isPickOnBounds = true
}
}
}
}
}
}
}
class TextImagen(val name : String, val url : String)