在JFXListView中使用单元格工厂会忽略JFoenix样式
使用JFoenix控件在JFXListView中使用单元格工厂会忽略JFoenix样式,listview,javafx,kotlin,javafx-8,jfoenix,Listview,Javafx,Kotlin,Javafx 8,Jfoenix,使用JFoenix控件JFXListView时,如果使用单元格工厂,则无法将材质样式指定给列表。如何使用单元格工厂,同时为JFXListView控件指定样式 以下是我的最小、完整且可验证的示例: demoStyle.css(摘自JFoenix演示) demoLayout.fxml <?import com.jfoenix.controls.JFXListView?> <?import javafx.scene.layout.HBox?> <HBox
JFXListView
时,如果使用单元格工厂,则无法将材质样式指定给列表。如何使用单元格工厂,同时为JFXListView
控件指定样式
以下是我的最小、完整且可验证的示例:
demoStyle.css(摘自JFoenix演示)
demoLayout.fxml
<?import com.jfoenix.controls.JFXListView?>
<?import javafx.scene.layout.HBox?>
<HBox
xmlns="http://javafx.com/javafx/8.0.112"
xmlns:fx="http://javafx.com/fxml"
fx:controller="<some>.<valid>.<package>.<structure>.DemoController"
minWidth="Infinity"
minHeight="Infinity"
>
<JFXListView fx:id="jfoenixListViewPlain"/>
<JFXListView fx:id="jfoenixListViewWithCustomCell"/>
</HBox>
Demo.kt
import com.jfoenix.controls.JFXListView
import javafx.application.Application
import javafx.collections.FXCollections
import javafx.fxml.FXML
import javafx.fxml.FXMLLoader
import javafx.fxml.Initializable
import javafx.scene.Scene
import javafx.scene.control.ContentDisplay
import javafx.scene.control.Label
import javafx.scene.control.ListCell
import javafx.scene.layout.HBox
import javafx.scene.paint.Color
import javafx.stage.Stage
import java.net.URL
import java.util.*
data class MyCustomData(val text: String)
class DemoController : Initializable
{
internal class MyCustomCell : ListCell<MyCustomData>()
{
init
{
text = null
contentDisplay = ContentDisplay.GRAPHIC_ONLY
}
override fun updateItem(item: MyCustomData?, empty: Boolean)
{
super.updateItem(item, empty)
graphic =
if (empty || item == null)
{
null
}
else
{
// In practice this isn't a Label but a Pane with multiple children
Label().apply { text = item.text }
}
}
}
@FXML
private lateinit var jfoenixListViewPlain: JFXListView<String>
@FXML
private lateinit var jfoenixListViewWithCustomCell: JFXListView<MyCustomData>
override fun initialize(location: URL?, resources: ResourceBundle?)
{
val plainList = FXCollections.observableArrayList("A", "B", "C")
jfoenixListViewPlain.items = plainList
val customItemsList = FXCollections.observableArrayList(MyCustomData("A"), MyCustomData("B"), MyCustomData("C"))
jfoenixListViewWithCustomCell.items = customItemsList
jfoenixListViewWithCustomCell.setCellFactory { MyCustomCell() }
}
}
class MainDemoClass : Application()
{
companion object
{
@JvmStatic
fun main(args: Array<String>)
{
Application.launch(MainDemoClass::class.java, *args)
}
}
@Throws(Exception::class)
override fun start(stage: Stage)
{
val loader = FXMLLoader(javaClass.getResource("/layouts/demoLayout.fxml"))
val root = loader.load<HBox>()
with(stage)
{
title = "Test's Title"
scene =
Scene(root, 600.0, 600.0, Color.WHITE).apply {
with(stylesheets)
{
add(MainDemoClass::class.java.getResource("/css/jfoenix-design.css").toExternalForm())
add(MainDemoClass::class.java.getResource("/css/demoStyle.css").toExternalForm())
}
}
isResizable = false
show()
}
}
}
import com.jfoenix.controls.JFXListView
导入javafx.application.application
导入javafx.collections.FXCollections
导入javafx.fxml.fxml
导入javafx.fxml.fxmloader
导入javafx.fxml.Initializable
导入javafx.scene.scene
导入javafx.scene.control.ContentDisplay
导入javafx.scene.control.Label
导入javafx.scene.control.ListCell
导入javafx.scene.layout.HBox
导入javafx.scene.paint.Color
导入javafx.stage.stage
导入java.net.URL
导入java.util*
数据类MyCustomData(val文本:字符串)
类DemoController:可初始化
{
内部类MyCustomCell:ListCell()
{
初始化
{
text=null
contentDisplay=contentDisplay.GRAPHIC\u仅限
}
重写fun updateItem(项:MyCustomData?,空:布尔值)
{
super.updateItem(项,空)
图形=
if(空| |项==null)
{
无效的
}
其他的
{
//实际上,这不是一个标签,而是一个包含多个子项的窗格
Label().apply{text=item.text}
}
}
}
@FXML
私有lateinit变量JFoenix ListViewPlain:JFXListView
@FXML
私有lateinit var jFoenix ListViewWithCustomCell:JFXListView
覆盖乐趣初始化(位置:URL?,资源:ResourceBundle?)
{
val plainList=FXCollections.observableArrayList(“A”、“B”、“C”)
jfoenixListViewPlain.items=plainList
val customItemsList=FXCollections.observearraylist(MyCustomData(“A”)、MyCustomData(“B”)、MyCustomData(“C”))
JFoenix ListViewWithCustomCell.items=customItemsList
jfoenixListViewWithCustomCell.setCellFactory{MyCustomCell()}
}
}
类MainDemoClass:Application()
{
伴星
{
@JvmStatic
趣味主线(args:Array)
{
launch(MainDemoClass::class.java,*args)
}
}
@抛出(异常::类)
覆盖乐趣开始(阶段:阶段)
{
val loader=FXMLLoader(javaClass.getResource(“/layouts/demoLayout.fxml”))
val root=loader.load()
有(舞台)
{
title=“测试的标题”
场面=
场景(根,600.0600.0,彩色。白色)。应用{
带有(样式表)
{
添加(MainDemoClass::class.java.getResource(“/css/jfoenix design.css”).toExternalForm())
添加(MainDemoClass::class.java.getResource(“/css/demoStyle.css”).toExternalForm())
}
}
isressizable=false
show()
}
}
}
更改内部类MyCustomCell:ListCell()
打开
内部类MyCustomCell:JFXListCell()
更改内部类MyCustomCell:ListCell()
打开
内部类MyCustomCell:JFXListCell()
import com.jfoenix.controls.JFXListView
import javafx.application.Application
import javafx.collections.FXCollections
import javafx.fxml.FXML
import javafx.fxml.FXMLLoader
import javafx.fxml.Initializable
import javafx.scene.Scene
import javafx.scene.control.ContentDisplay
import javafx.scene.control.Label
import javafx.scene.control.ListCell
import javafx.scene.layout.HBox
import javafx.scene.paint.Color
import javafx.stage.Stage
import java.net.URL
import java.util.*
data class MyCustomData(val text: String)
class DemoController : Initializable
{
internal class MyCustomCell : ListCell<MyCustomData>()
{
init
{
text = null
contentDisplay = ContentDisplay.GRAPHIC_ONLY
}
override fun updateItem(item: MyCustomData?, empty: Boolean)
{
super.updateItem(item, empty)
graphic =
if (empty || item == null)
{
null
}
else
{
// In practice this isn't a Label but a Pane with multiple children
Label().apply { text = item.text }
}
}
}
@FXML
private lateinit var jfoenixListViewPlain: JFXListView<String>
@FXML
private lateinit var jfoenixListViewWithCustomCell: JFXListView<MyCustomData>
override fun initialize(location: URL?, resources: ResourceBundle?)
{
val plainList = FXCollections.observableArrayList("A", "B", "C")
jfoenixListViewPlain.items = plainList
val customItemsList = FXCollections.observableArrayList(MyCustomData("A"), MyCustomData("B"), MyCustomData("C"))
jfoenixListViewWithCustomCell.items = customItemsList
jfoenixListViewWithCustomCell.setCellFactory { MyCustomCell() }
}
}
class MainDemoClass : Application()
{
companion object
{
@JvmStatic
fun main(args: Array<String>)
{
Application.launch(MainDemoClass::class.java, *args)
}
}
@Throws(Exception::class)
override fun start(stage: Stage)
{
val loader = FXMLLoader(javaClass.getResource("/layouts/demoLayout.fxml"))
val root = loader.load<HBox>()
with(stage)
{
title = "Test's Title"
scene =
Scene(root, 600.0, 600.0, Color.WHITE).apply {
with(stylesheets)
{
add(MainDemoClass::class.java.getResource("/css/jfoenix-design.css").toExternalForm())
add(MainDemoClass::class.java.getResource("/css/demoStyle.css").toExternalForm())
}
}
isResizable = false
show()
}
}
}