Kotlin 有没有办法在tornadofx中使用fxml文件创建自定义列表单元格?
有没有办法使用FX中的fxml文件创建ListView的自定义ListCell 我有一个fxml文件Kotlin 有没有办法在tornadofx中使用fxml文件创建自定义列表单元格?,kotlin,javafx,fxml,tornadofx,Kotlin,Javafx,Fxml,Tornadofx,有没有办法使用FX中的fxml文件创建ListView的自定义ListCell 我有一个fxml文件CustomListCell: <HBox> <Label text="File name"/> <ImageView fitHeight="18.0" fitWidth="18.0" pickOnBounds="true"/> <stylesheets> ... </stylesheets> &
CustomListCell
:
<HBox>
<Label text="File name"/>
<ImageView fitHeight="18.0" fitWidth="18.0" pickOnBounds="true"/>
<stylesheets>
...
</stylesheets>
</HBox>
为此,如何实现
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)
类示例:视图(“示例”){
val listofitem=FXCollections.observearraylist()
覆盖val root=vbox{
添加(TextImagen(“Car”、“Car.png”))
添加(TextImagen(“Apple”、“Apple.png”))
添加(TextImagen(“Pencil”、“Pencil.png”))
listview(ListoItem){
蜂窝格式{
图形=缓存(it){
hbox{
标签(it.name)
imageview(it.url){
fitHeight=18.0
fitWidth=18.0
isPickOnBounds=true
}
}
}
}
}
}
}
类TextImagen(val名称:String,val url:String)
您不应该使用UI类(CustomListCell
)作为列表视图的数据类型。创建一个封装名称和图像的类,并将其用作列表视图的类型。然后在ListView
上设置一个cellFactory
,以生成CustomListCell
实例来显示它。我不认识科特林,所以我不能详细回答这个问题。
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)