0

I'm currently trying to use a scala.swing.ListView in a Scala application.

I basically want to get the selected item of a ListView, as soon as the selection changes.

Here a sample which can be pasted into the Scala prompt.

import scala.swing._

class MainView extends MainFrame {

  import ListView._
  val selector = new ListView(List[Int]()) {
    renderer = Renderer(x => x.toString)
    selection.reactions += {
      case event.ListSelectionChanged(source, range, _) =>
        if(!range.isEmpty) {
          println("selection " + range)
        }
    }
    selection.intervalMode = IntervalMode.Single
  }

  selector.listData = 1 +: selector.listData
  selector.listData = 2 +: selector.listData
  selector.listData = 3 +: selector.listData
  selector.listData = 4 +: selector.listData
  selector.listData = 5 +: selector.listData

  contents = new GridBagPanel(){
    layout(new ScrollPane(selector))  = new Constraints() {
      gridx = 0
      gridy = 0
      weighty = 1
      fill = GridBagPanel.Fill.Vertical
    }
  }
  visible = true
}

var view = new MainView()

If I would now select the first element in the list, I would expect Range to contain only 0, if I then select the second element, I would expect only 1 and so on.

However, if I select the first and then the second element, Range contains (0, 1). If I select the fifth and then the third element, range contains (2, 3, 4).

So, range contains always the range between the last and the current selected item, in ascending order.

Sadly, the Scala documentation does not state how this API works.

Does anyone know what I'm doing wrong here? Might this be the wrong event for reacting on ListView selection changes?

4

0 回答 0