gpt4 book ai didi

swing - Scala Swing ListView 的移除/添加元素事件

转载 作者:行者123 更新时间:2023-12-04 21:23:51 27 4
gpt4 key购买 nike

引用此,

How do I change the contents of a ListView in Scala?

我可以换 ListView通过更改内容 listData .但是,我无法获得 ListView发布这些事件,ListElementsAdded , ListElementsAddedListChanged .从ListView的样子源,它只会向只读空模型添加一个监听器。我该怎么做?

谢谢

最佳答案

后来想办法让ListView发布这些事件,请引用代码。

这样做的权利吗?有一个更好的方法吗?请指教。

谢谢

** 代码借用和修改**

object ListViewTest extends SimpleSwingApplication
{
lazy val top = new MainFrame
{
title = "ListView Test"
contents = new BoxPanel(Orientation.Vertical)
{
border = Swing.EmptyBorder(2, 2, 2, 2)

val listModel = new DefaultListModel
List("First", "Second", "Third", "Fourth", "Fifth").map(listModel.addElement(_))
val myList = ListBuffer()
val listView = new ListView[String](myList)
{
selection.intervalMode = ListView.IntervalMode.Single
peer.setModel(listModel)

//listData = myList

}
listView.peer.getModel.addListDataListener(new ListDataListener {
def contentsChanged(e: ListDataEvent) { publish(ListChanged(listView)) }
def intervalRemoved(e: ListDataEvent) { publish(ListElementsRemoved(listView, e.getIndex0 to e.getIndex1)) }
def intervalAdded(e: ListDataEvent) { publish(ListElementsAdded(listView, e.getIndex0 to e.getIndex1)) }
})

contents += new ScrollPane(listView)

val label = new Label("No selection")
contents += label

val b = new Button("Remove")
contents += b

listenTo(listView.selection, listView, b)
reactions +=
{
case ListSelectionChanged(list, range, live) =>
label.text = "Selection: " + range
case e: ButtonClicked =>
if (listView.listData.isEmpty)
{
b.enabled = false
}
else
{
listView.peer.getModel.asInstanceOf[DefaultListModel].remove(listView.selection.anchorIndex)
}

case ListElementsRemoved(source, range) =>
println("Element at " + (range.start + 1) + " is removed.")
}
}
pack
}

关于swing - Scala Swing ListView 的移除/添加元素事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7139684/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com