简体   繁体   English

TornadoFx 将数据从 mysql 传递到表视图

[英]TornadoFx pass data from mysql to table view

I have a class to pass notes from mysql to tableview in kotlin but i cant seem to make it work Im a little new in kotlin for desktop, only used in android with firebase I have a class to pass notes from mysql to tableview in kotlin but i cant seem to make it work Im a little new in kotlin for desktop, only used in android with firebase

This is my class to get the notes这是我的 class 来获取笔记

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

then i have the actual code然后我有实际的代码

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

At the end I will try to solve your problem, but please, read this part first, because this is far more import for you than the actual answer.最后我会尝试解决你的问题,但是请先阅读这部分,因为这对你来说比实际答案更重要。 I believe your programing skills (for now) are not the required for the kind of things you are trying to accomplish, especially because you are converting your class to string before adding it to your data (which seem to be a collection of string not a collection of Notes), so I don't know how you expect the tableview will get your Id, Title and Description.我相信你的编程技能(目前)不是你想要完成的事情所必需的,特别是因为你在将 class 添加到数据之前将其转换为字符串(这似乎是字符串的集合而不是注释集合),所以我不知道您希望 tableview 如何获得您的 ID、标题和描述。 Also, you have a constructor for Notes, but you are overcomplicating things by not using it and assign values later.此外,您有一个 Notes 的构造函数,但是您不使用它并稍后分配值会使事情变得过于复杂。 In other hand, you getNotes() function is never call in your code, probably is called in some other part you are not showing.另一方面,您 getNotes() function 永远不会在您的代码中调用,可能在您未显示的其他部分中调用。

Because of that, I think you should slow down a little bit, try to level up your basic skills (specially working with classes and collections), them read the tornadofx manual, and them try with this kind of stuff.正因为如此,我认为你应该放慢一点,尝试提高你的基本技能(特别是使用类和集合),他们阅读 tornadofx 手册,然后他们尝试使用这种东西。

Now this is my solution.现在这是我的解决方案。 First try this without the database.首先在没有数据库的情况下尝试这个。 I did it this way because I don't know if there is any problem with your database.我这样做是因为我不知道您的数据库是否有任何问题。 Them change the getNotes() function to the way is in your code, without converting the notes.toString(), just de data.add(notes).他们将 getNotes() function 更改为代码中的方式,而不转换 notes.toString(),只需 de data.add(notes)。 Remember to click the button to load the data.请记住单击按钮以加载数据。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM