2017-09-27 8 views
0

我的应用程序需要允许添加到列表视图。我已经想通过使用observableArrayList来动态添加到列表视图。如果我点击按钮,一个项目被添加到列表并显示。如何在动态列表视图上安装点击处理程序(在tornadofx中)

现在我努力添加一个点击处理程序(我想处理当有人点击列表视图中的任何项目时发生的事件)。我在哪里做这个?

这是我的代码。

package someapp 

import javafx.collections.FXCollections 
import javafx.geometry.Pos 
import javafx.scene.layout.VBox 
import javafx.scene.text.FontWeight 
import tornadofx.* 

class MyApp : App(HelloWorld::class) { 
} 

class HelloWorld : View() { 
    val leftSide: LeftSide by inject() 

    override val root = borderpane { 
     left = leftSide.root 
    } 
} 

class LeftSide: View() { 

    var requestView: RequestView by singleAssign() 

    override val root = VBox() 

    init { 
     with(root) { 
      requestView = RequestView() 
      this += requestView 

      this += button("Add Item") { 
       action { 
        requestView.responses.add(Request("example.com", 
          "/foo/bar", 
          "{ \"foo\" : \"bar\"}".toByteArray())) 
       } 
      } 

     } 
    } 

} 


class RequestView : View() { 

    val responses = FXCollections.observableArrayList<Request>(
    ) 

    override val root = listview(responses) { 
     cellFormat { 
      graphic = cache { 
       form { 
        fieldset { 
         label(it.hostname) { 
          alignment = Pos.CENTER_RIGHT 
          style { 
           fontSize = 22.px 
           fontWeight = FontWeight.BOLD 
          } 
         } 
         field("Path") { 
          label(it.path) 
         } 

        } 
       } 
      } 
     } 

    } 

} 

class Request(val hostname: String, val path: String, val body: ByteArray) { 
} 

回答

2

配置在ListView的项目被选中时的回调,使用onUserSelect回调:

onUserSelect { 
    information("You selected $it") 
} 

您可以选择通过多少次点击构成选择为好,默认为2:

onUserSelect(1) { 
    information("You selected $it") 
} 

您使用一些过时的结构在你的代码,在这里被转换为最佳做法:)

的更新版本
class MyApp : App(HelloWorld::class) 

class HelloWorld : View() { 
    override val root = borderpane { 
     left(LeftSide::class) 
    } 
} 

class LeftSide : View() { 
    val requestView: RequestView by inject() 

    override val root = vbox { 
     add(requestView) 
     button("Add Item").action { 
      requestView.responses.add(Request("example.com", 
        "/foo/bar", 
        """{ "foo" : "bar"}""".toByteArray())) 
     } 
    } 
} 


class RequestView : View() { 
    val responses = FXCollections.observableArrayList<Request>() 

    override val root = listview(responses) { 
     cellFormat { 
      graphic = cache { 
       form { 
        fieldset { 
         label(it.hostname) { 
          alignment = Pos.CENTER_RIGHT 
          style { 
           fontSize = 22.px 
           fontWeight = FontWeight.BOLD 
          } 
         } 
         field("Path") { 
          label(it.path) 
         } 
        } 
       } 
      } 
     } 
     onUserSelect(1) { 
      information("You selected $it") 
     } 
    } 

} 

class Request(val hostname: String, val path: String, val body: ByteArray) 
+0

这太好了,谢谢。我非常感谢您了解最佳做法。但是,使用新的语法,我现在不完全确定如何获取视图的引用。例如,如果我想添加一个视图到HelloWorld(在正确的位置),那么我将如何连接'onUserSelect(1)',以便在那里我可以与右侧视图实例交谈?我对使用':: class'有点困惑,因为我认为我想要引用实例,而不是类(显然这是发生在引擎之下的,但我不清楚我如何引用实例化对象,和从哪里(init?))。 – xrd

+0

或者,新的语法记录在哪里? – xrd

+0

这条语法遍布指南。如果您发现使用过时语法的示例,请在指南项目中提出问题,我们将更新它:) –

相关问题