ListView 上的 Scala Swing 弹出菜单 [英] Scala Swing Popup menu on ListView

查看:34
本文介绍了ListView 上的 Scala Swing 弹出菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将 mouseListener 添加到 Scala Swing ListView 的最佳方法是,单击列表中的任何项目都会创建一个 PopupMenu,其中包含与所单击的特定项目相关的选项?

What would be the best way of adding a mouseListener to a Scala Swing ListView that what ever item in the list is clicked on it'll create a PopupMenu with options pertaining to that specific item that is clicked on?

我现在是坚持使用 Java 风格的代码来做这件事,还是自 2.8.1 以来 Scala Swing 进化得更多了

Am I stuck with doing this with Java style code for now or has Scala Swing evolved a bit more since 2.8.1

我目前得到的一些信息,也许我听错了,正在查看 ListView 上的 ScalaDocs.

A bit of what I got currently and maybe I'm listening to the wrong thing and am over looking the ScalaDocs on the ListView.

lazy val ui = new FlowPanel {
    val listView = ListView(items) {
        renderer = Renderer(_.name)
        listenTo(this.mouse.clicks)
        reactions += {
            case e: MouseClicked => 
                // How do I determine what item was clicked?
        }
    }
}

推荐答案

  lazy val ui = new FlowPanel { 
    val listView = new ListView( Seq("spam", "eggs", "ham") )
    listenTo(listView.selection)
    reactions += {
      case SelectionChanged(`listView`) => println(listView.selection.items(0))
    }
    contents += listView
  }

这应该产生诸如

spam
spam
eggs
eggs
ham
ham

当您点击各种项目时.我以前从未这样做过,但我查看了 UIDemo 示例,该示例可以在 scala.swing.test 包中找到.要阅读源代码,如果您有 IntelliJ,只需单击项目"窗格中外部库"中的 scala-swing.jar 中的相关对象即可.

as you click on the various items. I've never done this before but I had a look at the UIDemo example which can be found in the scala.swing.test package. To read the source, if you have IntelliJ, it's as simple as clicking on the relevant object in the scala-swing.jar in External Libraries in the Projects pane.

至于 PopupMenu,我不知道 - 看起来它在 2.9.1 中没有 scala-swing 包装器,但我在 GitHub 上找到了一个 这里.或者您可以只使用普通的 Swing 版本.

As for PopupMenu, I don't know - it doesn't look like that one has a scala-swing wrapper in 2.9.1, but I found one on GitHub here. Or you could just use the normal Swing version.

这篇关于ListView 上的 Scala Swing 弹出菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆