[英]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
這是我的 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)
}
然后我有實際的代碼
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")
}
}
最后我會嘗試解決你的問題,但是請先閱讀這部分,因為這對你來說比實際答案更重要。 我相信你的編程技能(目前)不是你想要完成的事情所必需的,特別是因為你在將 class 添加到數據之前將其轉換為字符串(這似乎是字符串的集合而不是注釋集合),所以我不知道您希望 tableview 如何獲得您的 ID、標題和描述。 此外,您有一個 Notes 的構造函數,但是您不使用它並稍后分配值會使事情變得過於復雜。 另一方面,您 getNotes() function 永遠不會在您的代碼中調用,可能在您未顯示的其他部分中調用。
正因為如此,我認為你應該放慢一點,嘗試提高你的基本技能(特別是使用類和集合),他們閱讀 tornadofx 手冊,然后他們嘗試使用這種東西。
現在這是我的解決方案。 首先在沒有數據庫的情況下嘗試這個。 我這樣做是因為我不知道您的數據庫是否有任何問題。 他們將 getNotes() function 更改為代碼中的方式,而不轉換 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"))
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.