scala - scalafx 和 TableView:如何更改 slectionModel 和 focusModel?
问题描述
我想TableView
在 scalafx 中使用一个在我的 GUI 中有一个漂亮的表,用户可以在其中查看和输入数据。TableViewSelectionModel
类中有一些不错的方法TableViewFocusModel
,我想使用它们,例如selectionMode
, selectedCells
and selectBelowCell
from TableViewSelectionModel
or focus(pos)
, focusedCell
and focusBelowCell
from TableViewFocusModel
。
我有以下示例,该示例主要来自 Mark Lewis 的 YouTube 视频(顺便说一下,他为 scala 和 scalafx 制作了出色的学习视频,但未涵盖此主题):
import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.beans.property.{ObjectProperty, StringProperty}
import scalafx.collections.ObservableBuffer
import scalafx.scene.Scene
import scalafx.scene.control.cell.TextFieldTableCell
import scalafx.scene.control.{TableColumn, TableView}
import scalafx.util.converter.DefaultStringConverter
object Main extends JFXApp {
case class Student(name: String, test1: Int, test2: Int)
val data = new ObservableBuffer[Student]()
data ++= List(Student("Jane Doe", 99, 93), Student("John Doe", 73, 88), Student("Bob Builder", 85, 91))
stage = new JFXApp.PrimaryStage {
title = "Test for Table View"
resizable = true
width = 1000
height = 800
val table: TableView[Student] = new TableView[Student](data) {
editable = true
//selectionModel = TableView.TableViewSelectionModel[Tables.Student] ???
//focusModel = TableView.TableViewFocusModel[Tables.Student] ???
}
println("selection model: " + table.selectionModel)
println("focus model: " + table.focusModel)
val col1: TableColumn[Student, String] = new TableColumn[Student, String] {
text = "Name"
editable = true
cellValueFactory = { cdf: TableColumn.CellDataFeatures[Student, String] => StringProperty(cdf.value.name) }
cellFactory = (_: TableColumn[Student, String]) => new TextFieldTableCell[Student, String](new DefaultStringConverter())
}
val col2: TableColumn[Student, Int] = new TableColumn[Student, Int] {
text = "Test 1"
editable = true
cellValueFactory = { cdf => ObjectProperty(cdf.value.test1) }
}
val col3: TableColumn[Student, Int] = new TableColumn[Student, Int] {
text = "Test 2"
editable = true
cellValueFactory = { cdf => ObjectProperty(cdf.value.test2) }
}
val col4: TableColumn[Student, Double] = new TableColumn[Student, Double] {
text = "Average"
cellValueFactory = { cdf => ObjectProperty((cdf.value.test1 + cdf.value.test2) / 2.0) }
}
val col5: TableColumn[Student, String] = new TableColumn[Student, String] {
text = "Test-Input"
editable = true
cellFactory = (_: TableColumn[Student, String]) => { new TextFieldTableCell[Student, String](new DefaultStringConverter())
}
}
table.columns ++= List(col1, col2, col3, col4, col5)
scene = new Scene {
root = table
}
}
}
我的问题是我不知道 java 或 javaFX。我试图从 javaFX 文档中获取信息,但这并没有真正的帮助。例如,javaFX 有一个用于.setSelectionMode(SelectionMode.MULTIPLE);
设置选择模式的表的方法,但是在 scalafx 中如何做到这一点?
如果有人可以修改上面的代码,这将对我有所帮助,以便选择单个单元格而不是整行。我很确定从那里我可以自己解决剩下的问题。
PS:我用sbt 1.2.8版本编译了上面的代码,我的build.sbt文件是:
name := "TestTableView"
scalaVersion := "2.13.5"
// Scala FX
libraryDependencies += "org.scalafx" %% "scalafx" % "15.0.1-R21"
libraryDependencies += "org.scala-lang.modules" %% "scala-xml" % "1.2.0"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.3"
fork := true
// Tell Javac and scalac for which jvm it has to build (not really necessary)
javacOptions ++= Seq("-source", "1.8", "-target", "1.8")
scalacOptions += "-target:jvm-1.8"
scalacOptions += "-feature"
解决方案
解决了错误突出显示的问题,解决方案很简单。仅供参考,这里是使用 TableView 的所有询问选项修改的上述代码:
import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.beans.property.{ObjectProperty, StringProperty}
import scalafx.collections.ObservableBuffer
import scalafx.scene.Scene
import scalafx.scene.control.SelectionMode.Multiple
import scalafx.scene.control.cell.TextFieldTableCell
import scalafx.scene.control.{TableColumn, TableView}
import scalafx.util.converter.DefaultStringConverter
object Main extends JFXApp {
case class Student(name: String, test1: Int, test2: Int)
val data = new ObservableBuffer[Student]()
data ++= List(Student("Jane Doe", 99, 93), Student("John Doe", 73, 88), Student("Bob Builder", 85, 91))
stage = new JFXApp.PrimaryStage {
title = "Test for Table View"
resizable = true
width = 1000
height = 800
val table: TableView[Student] = new TableView[Student](data) {
editable = true
selectionModel.apply.cellSelectionEnabled = true
selectionModel().selectionMode = Multiple //Choose between Single and Multiple
selectionModel.apply.selectedItem.onChange {
println("Selected" + selectionModel.apply.getSelectedItem)
println("Selected row-Index: " + selectionModel.apply.selectedIndex.value)
println("Selected cells: " + table.selectionModel().selectedCells)
println("Focused Cell: " + table.focusModel().focusedCell)
}
}
val col1: TableColumn[Student, String] = new TableColumn[Student, String] {
text = "Name"
editable = true
cellValueFactory = { cdf: TableColumn.CellDataFeatures[Student, String] => StringProperty(cdf.value.name) }
cellFactory = (_: TableColumn[Student, String]) => new TextFieldTableCell[Student, String](new DefaultStringConverter())
}
val col2: TableColumn[Student, Int] = new TableColumn[Student, Int] {
text = "Test 1"
editable = true
cellValueFactory = { cdf: TableColumn.CellDataFeatures[Student, Int] => ObjectProperty(cdf.value.test1) }
//cellFactory = (_: TableColumn[Student, String]) => new TextFieldTableCell[Student, String](new DefaultStringConverter())
}
val col3: TableColumn[Student, Int] = new TableColumn[Student, Int] {
text = "Test 2"
editable = true
cellValueFactory = { cdf => ObjectProperty(cdf.value.test2) }
}
val col4: TableColumn[Student, Double] = new TableColumn[Student, Double] {
text = "Average"
cellValueFactory = { cdf => ObjectProperty((cdf.value.test1 + cdf.value.test2) / 2.0) }
}
val col5: TableColumn[Student, String] = new TableColumn[Student, String] {
text = "Test-Input"
editable = true
cellFactory = (_: TableColumn[Student, String]) => {
new TextFieldTableCell[Student, String](new DefaultStringConverter())
}
}
table.columns ++= List(col1, col2, col3, col4, col5)
table.requestFocus()
table.selectionModel().select(1, col1) //Select 2'nd (count starts at 0) cell in col2, that is Test1
table.focusModel().focus(1, col1) //Set focus on
table.edit(1, col1) //wander what this is doing
table.selectionModel().selectBelowCell() //Selects the cell below
table.focusModel().focusBelowCell() //Focuses on the cell below
scene = new Scene {
root = table
}
}
推荐阅读
- elasticsearch - 从 Logstash 中的多个目录生成日志。日志未出现在 ElasticSearch 中
- python - matlibplot中两个向量之间的填充区域
- python - html web应用程序类型错误与python
- jquery - 我无法得到小计。带有 jquery 的购物车
- google-cloud-firestore - 无法为 Firebase 上的结算帐户添加信用卡/借记卡
- flutter - 错误:应为“地图”类型的值
',但得到了“列表”类型之一 ' - amazon-web-services - aws api 网关和 lambda cors 配置 HTTP api
- javascript - 如何在循环中打开这段代码
- pytorch - 预期 4 维输入,但得到 3 维输入
- npm - 执行 npm install gulp-sass 时出现 node-gyp 错误