gpt4 book ai didi

listview - 在JFXListView中使用单元工厂将忽略JFoenix样式

转载 作者:行者123 更新时间:2023-12-02 13:01:05 25 4
gpt4 key购买 nike

使用JFoenix控件JFXListView时,如果使用单元格工厂,则无法将 Material 样式分配给列表。如何使用单元格工厂,同时指定JFXListView控件的样式?

这是我的最小,完整和可验证的示例:

demoStyle.css (摘自JFoenix演示)

.jfx-list-cell-container {
-fx-alignment: center-left;
}

.jfx-list-cell-container > .label {
-fx-text-fill: BLACK;
}

.jfx-list-cell:odd:selected > .jfx-rippler > StackPane, .jfx-list-cell:even:selected > .jfx-rippler > StackPane {
-fx-background-color: rgba(0, 0, 255, 0.2);
}

.jfx-list-cell {
-fx-background-insets: 0.0;
-fx-text-fill: BLACK;
}

.jfx-list-cell:odd, .jfx-list-cell:even {
-fx-background-color: WHITE;
}

.jfx-list-cell:filled:hover {
-fx-text-fill: black;
}

.jfx-list-cell .jfx-rippler {
-jfx-rippler-fill: BLUE;
}

.jfx-list-view {
-fx-background-insets: 0;
-jfx-cell-horizontal-margin: 0.0;
-jfx-cell-vertical-margin: 5.0;
-jfx-vertical-gap: 10;
-jfx-expanded: false;
-fx-pref-width: 200;
}

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()
}
}
}

最佳答案

在上更改internal class MyCustomCell : ListCell<MyCustomData>()internal class MyCustomCell : JFXListCell<MyCustomData>()

关于listview - 在JFXListView中使用单元工厂将忽略JFoenix样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50297352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com