Scala Swing popup menu in ListView

What would be the best way to add a mouseListener to a Scala Swing ListView so that when all the items in the list are clicked, a PopupMenu will be created with the parameters related to that particular item that is clicked?

I am stuck with this using Java style code or now has Scala Swing evolved a bit more with 2.8.1

A bit of what I got at the moment, and maybe I'm listening to the wrong thing, and I'm watching ScalaDocs in a 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? } } } 
+4
source share
2 answers
  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 } 

This should produce a result, e.g.

 spam spam eggs eggs ham ham 

when you click on various items. I have never done this before, but I looked at the UIDemo example, which can be found in the scala.swing.test package. To read the source, if you have IntelliJ, it is as simple as clicking on the corresponding object in scala -swing.jar in the external libraries in the Projects panel.

As for PopupMenu, I don't know - it looks like it doesn't have a scala -swing wrapper in 2.9.1, but I found it on GitHub. Or you can just use the regular version of Swing.

+1
source

This is unacceptable late, but I have to offer a “Java-style solution” for anyone who might be interested (apart from the specific details of my implementation, the essence is in “reactions”):

  val listView = new ListView[Int] { selection.intervalMode = ListView.IntervalMode.Single listData = (1 to 20).toList listenTo(mouse.clicks) reactions += { case m : MouseClicked if m.clicks == 2 => doSomethingWith( listData(selection.anchorIndex) ) //where doSomethingWith is your desired result of this event } } 

Assuming single spacing mode, the key to getting a list item that was just a double click is to use anchorIndex simply.

0
source

Source: https://habr.com/ru/post/1383355/


All Articles