scalafx 和 TableView:如何更改 slectionModel 和 focusModel?
scalafx and TableView: how do I change slectionModel and focusModel?
我想在 scalafx 中使用 TableView
在我的 GUI 中有一个漂亮的 table,用户可以在其中查看和输入数据。我想使用 类 TableViewSelectionModel
和 TableViewFocusModel
中的一些不错的方法,例如 selectionMode
、selectedCells
和 selectBelowCell
来自 TableViewSelectionModel
或 focus(pos)
、focusedCell
和 focusBelowCell
来自 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 有一个方法 tables .setSelectionMode(SelectionMode.MULTIPLE);
设置选择模式,但我如何在 scalafx 中执行此操作?
如果有人可以修改上面的代码,这将对我有所帮助,以便选择单个单元格而不是整行。我很确定我可以自己解决剩下的问题。
P.S.: 我用 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
}
}
我想在 scalafx 中使用 TableView
在我的 GUI 中有一个漂亮的 table,用户可以在其中查看和输入数据。我想使用 类 TableViewSelectionModel
和 TableViewFocusModel
中的一些不错的方法,例如 selectionMode
、selectedCells
和 selectBelowCell
来自 TableViewSelectionModel
或 focus(pos)
、focusedCell
和 focusBelowCell
来自 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 有一个方法 tables .setSelectionMode(SelectionMode.MULTIPLE);
设置选择模式,但我如何在 scalafx 中执行此操作?
如果有人可以修改上面的代码,这将对我有所帮助,以便选择单个单元格而不是整行。我很确定我可以自己解决剩下的问题。
P.S.: 我用 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
}
}