Kotlin TornadFX将数据从mysql传递到表视图
我有一个类可以将mysql中的注释传递到kotlin中的tableview,但我似乎无法让它工作 我对kotlin的桌面有点新,只在android和firebase中使用 这是我的课,去拿笔记Kotlin TornadFX将数据从mysql传递到表视图,kotlin,javafx,tableview,tornadofx,Kotlin,Javafx,Tableview,Tornadofx,我有一个类可以将mysql中的注释传递到kotlin中的tableview,但我似乎无法让它工作 我对kotlin的桌面有点新,只在android和firebase中使用 这是我的课,去拿笔记 class Notes(id_notes: Int = 0, title: String = "none", description: String = "none"){ private var id_notes: SimpleIntegerProperty = S
class Notes(id_notes: Int = 0, title: String = "none", description: String = "none"){
private var id_notes: SimpleIntegerProperty = SimpleIntegerProperty(id_notes)
private var title: SimpleStringProperty = SimpleStringProperty(title)
private var description: SimpleStringProperty = SimpleStringProperty(description)
fun getId(): Int {
return id_notes.get()
}
fun setId(id: Int) {
id_notes.set(id)
}
fun getTitle(): String {
return title.get()
}
fun setTitle(Title: String) {
title.set(Title)
}
fun getDescription(): String {
return description.get()
}
fun setDescription(Description: String) {
description.set(Description)
}
然后我有了实际的代码
tableview(data){
prefWidth = 400.0
column("ID", Notes::getId)
column("Title", Notes::getTitle)
rowExpander {
label {
this.text = Notes::getDescription.toString()
}
}
}
private fun getNotes(){
try {
val notes = Notes()
val sql = ("SELECT id_notes, title, description, date FROM notes")
val con: Connection? = Conn.connection()
stmt = con?.createStatement()
rs = stmt?.executeQuery(sql)
while (rs!!.next()) {
notes.setId(rs!!.getInt("id_notes"))
notes.setDescription(rs!!.getString("description"))
notes.setTitle(rs!!.getString("title"))
data.add(notes.toString())
}
} catch (ex: SQLException) {
alert(Alert.AlertType.ERROR, "Error", "Could not perform this action")
}
}
最后,我将尝试解决您的问题,但请先阅读这一部分,因为这比实际答案更重要。我相信你的编程技能(目前)并不是你想要完成的事情所必需的,特别是因为你在将类添加到数据(似乎是字符串的集合而不是注释的集合)之前将其转换为字符串,所以我不知道你希望tableview如何获得你的Id,标题和说明。 此外,您还有一个Notes构造函数,但由于不使用它而在以后赋值,这会使事情变得过于复杂。另一方面,getNotes()函数永远不会在代码中被调用,很可能在您未显示的其他部分中被调用 因此,我认为你应该慢一点,试着提高你的基本技能(特别是处理类和集合),让他们阅读tornadofx手册,然后让他们尝试这类东西 这就是我的解决方案。首先,在不使用数据库的情况下尝试此操作。我这样做是因为我不知道你的数据库是否有问题。它们将getNotes()函数更改为代码中的方式,而无需转换notes.toString(),只需de data.add(notes)。请记住单击按钮以加载数据
class Prueba: View("MainView") {
//data should be an FXCollections.observableArrayList<Notes>
//You didn't show your data variable type, but apparently is some collection of string
val data = FXCollections.observableArrayList<Notes>()
override val root = vbox {
tableview(data){
prefWidth = 400.0
column("ID", Notes::getId)
column("Title", Notes::getTitle)
rowExpander() {
label() {
//Note the difference here, Notes::getDescription.toString() won't do what you want
this.text = it.getDescription()
}
}
}
//This button is calling the function getNotes(), so data can be loaded
button("Load Data") {
action {
getNotes()
}
}
}
//Note this function is out side root now
private fun getNotes() {
data.clear()
data.add(Notes(1,"Title 1", "Description 1"))
data.add(Notes(2,"Title 2", "Description 2"))
data.add(Notes(3,"Title 3", "Description 3"))
}
}
class-Prueba:View(“MainView”){
//数据应为FXCollections.observableArrayList
//您没有显示数据变量类型,但显然是字符串的集合
val data=FXCollections.observableArrayList()
覆盖val root=vbox{
tableview(数据){
预宽=400.0
列(“ID”,Notes::getId)
列(“标题”,注释::getTitle)
行扩展器(){
标签(){
//注意这里的区别,Notes::getDescription.toString()不会执行您想要的操作
this.text=it.getDescription()
}
}
}
//此按钮正在调用函数getNotes(),以便可以加载数据
按钮(“加载数据”){
行动{
getNotes()
}
}
}
//注意,这个函数现在是外部根
私人娱乐笔记(){
data.clear()
添加数据(注(1,“标题1”,“说明1”))
添加数据(注(2,“标题2”,“说明2”))
添加数据(注(3,“标题3”,“说明3”))
}
}
最后,我将尝试解决您的问题,但请先阅读这一部分,因为这比实际答案更重要。我相信你的编程技能(目前)并不是你想要完成的事情所必需的,特别是因为你在将类添加到数据(似乎是字符串的集合而不是注释的集合)之前将其转换为字符串,所以我不知道你希望tableview如何获得你的Id,标题和说明。
此外,您还有一个Notes构造函数,但由于不使用它而在以后赋值,这会使事情变得过于复杂。另一方面,getNotes()函数永远不会在代码中被调用,很可能在您未显示的其他部分中被调用
因此,我认为你应该慢一点,试着提高你的基本技能(特别是处理类和集合),让他们阅读tornadofx手册,然后让他们尝试这类东西
这就是我的解决方案。首先,在不使用数据库的情况下尝试此操作。我这样做是因为我不知道你的数据库是否有问题。它们将getNotes()函数更改为代码中的方式,而无需转换notes.toString(),只需de data.add(notes)。请记住单击按钮以加载数据
class Prueba: View("MainView") {
//data should be an FXCollections.observableArrayList<Notes>
//You didn't show your data variable type, but apparently is some collection of string
val data = FXCollections.observableArrayList<Notes>()
override val root = vbox {
tableview(data){
prefWidth = 400.0
column("ID", Notes::getId)
column("Title", Notes::getTitle)
rowExpander() {
label() {
//Note the difference here, Notes::getDescription.toString() won't do what you want
this.text = it.getDescription()
}
}
}
//This button is calling the function getNotes(), so data can be loaded
button("Load Data") {
action {
getNotes()
}
}
}
//Note this function is out side root now
private fun getNotes() {
data.clear()
data.add(Notes(1,"Title 1", "Description 1"))
data.add(Notes(2,"Title 2", "Description 2"))
data.add(Notes(3,"Title 3", "Description 3"))
}
}
class-Prueba:View(“MainView”){
//数据应为FXCollections.observableArrayList
//您没有显示数据变量类型,但显然是字符串的集合
val data=FXCollections.observableArrayList()
覆盖val root=vbox{
tableview(数据){
预宽=400.0
列(“ID”,Notes::getId)
列(“标题”,注释::getTitle)
行扩展器(){
标签(){
//注意这里的区别,Notes::getDescription.toString()不会执行您想要的操作
this.text=it.getDescription()
}
}
}
//此按钮正在调用函数getNotes(),以便可以加载数据
按钮(“加载数据”){
行动{
getNotes()
}
}
}
//注意,这个函数现在是外部根
私人娱乐笔记(){
data.clear()
添加数据(注(1,“标题1”,“说明1”))
添加数据(注(2,“标题2”,“说明2”))
添加数据(注(3,“标题3”,“说明3”))
}
}
谢谢,我已经用FX系列解决了这个问题,等等,我简直是哑口无言,深夜根本没有思考我在写我不应该写的代码,我已经改变了整个页面!并不是说我不知道我是如何做了一件事而没有给予任何暗示,尽管我仍然感谢你的回答以及你是如何做的cared@JoãoMeloGarrido你说:“谢谢你的回答,谢谢你的关心”,但我的回答连一张赞成票都没有。是的!!!抱歉,因为我在度假,在我的电话上看到了这个谢谢,我已经用FX collection解决了这个问题,等等,我简直是哑口无言,深夜根本没有思考我在写我不应该写的代码,我已经改变了整个页面!并不是说我不知道我是如何做了一件事而没有任何暗示,尽管我仍然