kotlin - TornadoFX - remove item with ContextMenu right click option -
so have table view displays observedarraylist
of accountsaccount(name, login, pass)
, data classes. when right click cell there pops option of delete. want delete account
observedarraylist
only can not find way this. not experienced javafx or tornadofx , can't find answer google or in tornadofx guides , docs.
this code:
class toolview : view() { override val root = vbox() companion object handler { //val account1 = account("google", "martvdham@gmail.com", "kkk") //val account2 = account("google", "martvdham@gmail.com", "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 item delete code should be} } } } } bottom{ button("add account").setonaction{ replacewith(addview::class, viewtransition.slidein) } } } } }
thanks!
to clarify @martacus's answer, in case need replace // here item delete code should be
accounts.remove(this)
, you're in business.
you replace line
selecteditem?.apply{ accounts.remove(this) }
with
selecteditem?.let{ accounts.remove(it) }
from experience, let
more common apply
when using value instead of setting receiver.
note process different if accounts
list constructed asynchronously , copied in, default behavior of asyncitems { accounts }
.
Comments
Post a Comment