Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 有没有办法在tornadofx中使用fxml文件创建自定义列表单元格?_Kotlin_Javafx_Fxml_Tornadofx - Fatal编程技术网

Kotlin 有没有办法在tornadofx中使用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> &

有没有办法使用FX中的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是一个好主意,但通过这个示例,您可以得到您想要的结果:

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)