java.swing.AbstractListModel 中的 "fireContentsChanged" 方法究竟如何使用?

How exactly do I use the "fireContentsChanged" method in java.swing.AbstractListModel?

我如何在 java.swing.AbstractListModel 中使用 fireContentsChanged() 方法?当某些 ListModel 的元素的 "contents" 发生变化时,将调用此方法。

  1. 这是否仅仅意味着索引的对象发生了某种变化?
  2. 或者它是否还包括索引的对象已被其他对象替换的情况?
  3. 您可以将其用于 added/removed 索引吗?例如,假设我从列表中的任意位置删除了 10 个随机元素。我给什么 index0index1?如果我在随机位置插入 10 个元素会怎样?

AbstractListModel is an abstract implementation of ListModel that provides concrete implementations of the ListDataListener methods, but it contains no specific data structure internally. Receipt of a corresponding ListDataEvent allows the listening JList to update itself in response to a change in the ListModel. DefaultListModel is a typical concrete subclass of AbstractListModel that manipulate a Vector internally. The source illustrates typical usage. In particular, fireContentsChanged() is "Sent when the contents of the list has changed in a way that's too complex to characterize with the previous methods," fireIntervalAdded() or fireIntervalRemoved(). Because Vector is a legacy 的原始 DefaultTableModel,你会想要使用更灵活的替代方案; index0index1 引用您选择的数据结构的元素。

What if I insert 10 elements in random places?

那么index0index1应该"bracket the change."

What does "bracket the change" mean?

在此上下文中,bracket用作动词,意思是包围或包含; index0 应包含最低的更改索引,index1 应包含最高的更改索引。该范围可能包括中间单元格,即使它们没有更改。令人高兴的是,像 JTableJList 只渲染 visible 个单元格,因此边际成本是有限的;更多 .