所以我有一個表格視圖顯示observedArrayList
帳戶Account(name, login, pass)
,那些是數據類。當我右鍵單擊一個單元格時,彈出一個刪除選項。我想要做的就是從observedArrayList
刪除Account
TornadoFX - 刪除項目與ContextMenu右鍵單擊選項
只有我找不到任何方式來做到這一點。我對JavaFX或TornadoFX沒有經驗,我也無法在Google或TornadoFX指南和文檔中找到答案。
這是我的代碼:
class ToolView : View() {
override val root = VBox()
companion object handler {
//val account1 = Account("Google", "[email protected]", "kkk")
//val account2 = Account("Google", "[email protected]", "Password")
var accounts = FXCollections.observableArrayList<Account>(
)
var gson = GsonBuilder().setPrettyPrinting().create()
val ggson = Gson()
fun writeData(){
FileWriter("accounts.json").use {
ggson.toJson(accounts, it)
}
}
fun readData(){
accounts.clear()
FileReader("accounts.json").use{
var account = gson.fromJson(it, Array<Account>::class.java)
if(account == null){return}
for(i in account){
accounts.add(i)
}
}
}
}
init {
readData()
borderpane {
center {
tableview<Account>{
items = accounts
column("Name", Account::name)
column("Login", Account::login)
column("Password", Account::password)
contextMenu = ContextMenu().apply{
menuitem("Delete"){
selectedItem?.apply{// HERE IS WHERE THE ITEM DELETE CODE SHOULD BE}
}
}
}
}
bottom{
button("Add account").setOnAction{
replaceWith(AddView::class, ViewTransition.SlideIn)
}
}
}
}
}
謝謝!
有關語法的簡要說明:) TornadoFX有一個ContextMenu的構建器,所以不用'contextMenu = ContextMenu()。apply {'只需編寫'contextmenu {'。 –