- Java锁的逻辑(结合对象头和ObjectMonitor)
- 还在用饼状图?来瞧瞧这些炫酷的百分比可视化新图形(附代码实现)⛵
- 自动注册实体类到EntityFrameworkCore上下文,并适配ABP及ABPVNext
- 基于Sklearn机器学习代码实战
原文: TornadoFx的TableView组件使用 - Stars-One的杂货小窝 。
最近慢慢地接触了JavaFx中的TableView的使用,记下笔记总结 。
TornadoFx中对tableView进行了封装,我们只需要定义一个data class就可以使用其功能了(由于是Kotlin编写,所以没有兼容传统的Java类) 。
代码如下所示
/**
* Person
*
* @property name 姓名
* @property age 年龄
* @property type 类型 1:学生 2:老师
* @constructor Create empty Person
*/
data class Person(var name: String, var age: Int, var type: Int)
class TableViewDemo : View() {
val personList = observableListOf<Person>()
override val root = vbox {
setPrefSize(500.0,300.0)
tableview(personList) {
readonlyColumn("姓名",Person::name)
readonlyColumn("年龄",Person::age)
readonlyColumn("职位",Person::type)
}
personList.add(Person("张三", 12, 1))
personList.add(Person("李四", 12, 2))
}
}
显示效果
这个实际比较简单, 修改 prefWidth 属性即可 。
tableview(personList) {
readonlyColumn("姓名",Person::name){
prefWidth = 200.0
}
readonlyColumn("年龄",Person::age)
readonlyColumn("职位",Person::type)
}
效果
上述由于我们的职位是使用1和2来定义,想要把此数值在TableView中显示为对应的文本, 可以通过修改 cellFormat{} 函数中的 text 属性实现 。
示例代码如下
tableview(personList) {
readonlyColumn("姓名",Person::name){
prefWidth = 200.0
}
readonlyColumn("年龄",Person::age)
readonlyColumn("职位",Person::type){
cellFormat {
//这个it实际为当前行的对象的type属性
val temp = if (it==1){
"学生"
}else{
"老师"
}
text = temp
}
}
}
效果
比如说我们想要加个操作一栏,然后单元格里不要显示文字,而是显示一个按钮或者其他组件等, 可以通过 cellFormat{} 函数中的 graphic 属性来实现 。
示例代码如下
tableview(personList) {
readonlyColumn("姓名", Person::name) {
prefWidth = 200.0
}
readonlyColumn("年龄", Person::age)
readonlyColumn("职位", Person::type) {
cellFormat {
//这个it实际为当前行的对象的type属性
val temp = if (it == 1) {
"学生"
} else {
"老师"
}
text = temp
}
}
readonlyColumn("操作", Person::type) {
cellFormat {
val button = button("测试") {
action {
//当前行的数据对象
val item = items[index]
println(item)
}
}
//设置单元格显示按钮
graphic = button
}
}
}
效果
通过 tableview 对象的 placeholder 属性来设置 。
class TableViewDemo : View() {
val personList = observableListOf<Person>()
override val root = vbox {
setPrefSize(500.0, 300.0)
tableview(personList) {
readonlyColumn("姓名", Person::name) {
prefWidth = 200.0
}
readonlyColumn("年龄", Person::age)
readonlyColumn("职位", Person::type) {
cellFormat {
//这个it实际为当前行的对象的type属性
val temp = if (it == 1) {
"学生"
} else {
"老师"
}
text = temp
}
}
readonlyColumn("操作", Person::type) {
cellFormat {
val button = button("测试") {
action {
//当前行的数据对象
val item = items[index]
println(item)
}
}
//设置单元格显示按钮
graphic = button
}
}
//设置占位节点
placeholder =tablePlaceNode()
}
//显示空数据,注释掉数据添加逻辑
//personList.add(Person("张三", 12, 1))
//personList.add(Person("李四", 12, 2))
}
//这里方便管理,就抽取封装成一个方法了
private fun tablePlaceNode(): VBox {
return vbox{
alignment = Pos.CENTER
imageview("my_no_data.png"){
fitWidth = 200.0
fitHeight = 200.0
}
label("暂无数据")
}
}
}
效果
至于其他的,类似多选,右键出现菜单,单元格可显示输入框输入等逻辑,目前没怎么用到,暂时就省略不写了,有时间再来补充了 。
各位想要实现的话可以查看下TornadoFx的文档研究 。
.table-view {
-fx-selection-bar: rgba(255, 255, 255, 1);
-fx-selection-bar-non-focused: rgba(255, 255, 255, 1);
-fx-border-color: rgba(193, 197, 205, 1) rgba(193, 197, 205, 1) rgba(193, 197, 205, 1) rgba(193, 197, 205, 1);
-fx-border-width: 1px 1px 1px 1px;
-fx-background-insets: 0px 0px 0px 0px;
}
.table-view .column-header {
-fx-background-color: rgba(255, 255, 255, 1);
-fx-padding: 10px 5px 5px 5px;
-fx-border-color: rgba(235, 238, 245, 1) rgba(235, 238, 245, 1) rgba(235, 238, 245, 1) rgba(235, 238, 245, 1);
-fx-border-width: 0px 0.5px 0px 0.5px;
}
.table-view .filler {
-fx-background-color: rgba(255, 255, 255, 1);
}
.table-view .table-row-cell {
-fx-border-color: rgba(235, 238, 245, 1) rgba(235, 238, 245, 1) rgba(235, 238, 245, 1) rgba(235, 238, 245, 1);
-fx-border-width: 0.5px 0px 0.5px 0px;
}
.table-view .table-row-cell:odd {
-fx-background-color: rgba(250, 250, 250, 1);
}
.table-view .table-row-cell:hover {
-fx-background-color: rgba(245, 247, 250, 1);
}
.table-view .table-row-cell:selected {
-fx-background-color: rgba(236, 245, 255, 1);
}
.table-view .table-row-cell:selected .text {
-fx-fill: rgba(0, 0, 0, 1);
}
.table-view .table-cell {
-fx-padding: 10px 10px 10px 10px;
-fx-font-size: 13px;
}
.table-view .table-cell:selected {
-fx-text-fill: rgba(0, 0, 0, 1);
}
.table-view:focused {
-fx-border-color: rgba(50, 150, 255, 1) rgba(50, 150, 255, 1) rgba(50, 150, 255, 1) rgba(50, 150, 255, 1);
}
css来自 chenfei-javafx-css: javafx css 美化 。
importStylesheet("/css/tableview.css")
效果
最后此篇关于TornadoFx的TableView组件使用的文章就讲到这里了,如果你想了解更多关于TornadoFx的TableView组件使用的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
在我的 TornadoFX 应用程序的 View 中,我有一个边框 Pane ,在 View 的左侧和右侧有一个 VBox(中间没有任何内容)。现在,每个 VBox 都有相同的背景颜色,所以它们混合在
在我的 TornadoFX 应用程序的 View 中,我有一个边框 Pane ,在 View 的左侧和右侧有一个 VBox(中间没有任何内容)。现在,每个 VBox 都有相同的背景颜色,所以它们混合在
我的 build.gradle 看起来像这样: plugins { id 'org.jetbrains.kotlin.multiplatform' version '1.3.41' } kot
我已经开始工作 javafx .我想知道 javafx 之间的区别和 Tornadofx .它们之间是如何相互关联和不同的。 还有什么时候用什么。 有没有javafx的情况比 Tornadofx 更有
我想创建一个具有部分透明背景的 View (舞台、窗口)。我有一张包含 Alpha channel 的图像 我在JavaFx中使用了这种场景,我必须将场景填充设置为空,并将根节点背景颜色设置为透明。我
我想知道与TornadoFx相比使用另一功能是否有任何性能优势? 最佳答案 类型安全CSS可以与FXML和类型安全构建器一起使用。您需要将FXML与类型安全构建器进行比较。由于语法的噪音较低,因此生成
我目前遇到的问题之一是我似乎无法更改在动态加载的 AnchorPane 中实现的 Label 的文本。 .我认为这可能与 Controller 以及 JavaFX/TornadoFX 的工作方式有关,
我正在尝试更改选中的背景颜色的行和列表 View 的背景颜色,就像当我执行cell{backgroundColor += Color.BLACK}时一样,但是它删除了或者至少使选择颜色变黑了,我尝试了
在tornadofx 中,我试图验证形式中输入的两个值是否相等。我遵循了this指南,一切正常。但是我遇到了我无法检查输入中的两个值是否相等的方法。 例如,假设我要创建一个简单的注册表单,在该表单中,
是否有类似onFocus()方法的东西,可以像 View 类中的onDock()和onCreate()那样重写? 在文档中只写了关于live reloading的 View 。 我尝试将其与onDoc
我想创建一个具有部分透明背景的 View (舞台、窗口)。我有一张包含 Alpha channel 的图像 我在JavaFx中使用了这种场景,我必须将场景填充设置为空,并将根节点背景颜色设置为透明。我
读完文档后,我仍然对如何在另一个动画完成后执行动画感到有点困惑。我有一个这样的时间表: timeline { keyframe(Duration.seconds(0.5)) { keyva
我是 kotlin 和 tornadoFX 的新手。在 TornadoFX 指南中,可以使用以下内容构建 ToggleButton: togglebutton("OFF").action { text
我一直在尝试 Tornadofx。尝试创建自定义标题栏,这是我目前正在尝试的代码 fun main(args: Array) { launch(args) } class MyApp : Ap
我的任务很简单,但是做不到。 我有Kotlin | Tornadofx应用程序。 我打开fxml屏幕: class MainView : View() { override val root
我在TornadoFX(Kotlin)中有这个TableView: class MainView : View() { val persone = listOf( Pe
当 TornadoFX 窗口关闭时,如何关闭整个应用程序? 对于 JavaFX,通常是: val app = JFrame() app.defaultCloseOperation = JFrame.E
我正在尝试我的第一个应用程序 TornadoFx ,所以我从这段代码开始: package no.tornado.fxsample.workspace import javafx.applicatio
我在 IntelliJ CE 中完成了一个小 tornadofx 应用程序的编写,现在想将其导出为单个文件,供用户下载和启动。现在提供一个 jar 文件就足够了。但是了解如何为 OSX、Windows
我在使用 TorandoFX 找出相对简单的过滤配置时遇到一些困难。我想创建一个基于 SimpleStringProperty 的 FilteredList (由 ObservableList 支持)
我是一名优秀的程序员,十分优秀!