java - ListSelectionModel.insertIndexInterval() 究竟在做什么?

标签 java swing

我正在尝试编写自己的 ListSelectionModel 实现目前,我在尝试实现 insertIndexInterval 时遇到了困难。我不明白这个方法在 Sun/Oracle 的 DefautListSelectionModel 实现中的结果。这是一个例子:

ListSelectionModel model = new DefaultListSelectionModel();
model.setSelectionInterval(3, 5);
model.addListSelectionListener(new ListSelectionListener()
{
    public void valueChanged(ListSelectionEvent e)
    {
        System.out.println("Changed range reported by event: " +
            e.getFirstIndex() + "-" + e.getLastIndex());
    }
});

System.out.print("Selected indices before insert: ");
for (int i = model.getMinSelectionIndex(); i <= model.getMaxSelectionIndex(); i++)
    if (model.isSelectedIndex(i)) System.out.print(i + " ");
System.out.println();

model.insertIndexInterval(3, 3, true);  

System.out.print("Selected indices after insert: ");
for (int i = model.getMinSelectionIndex(); i <= model.getMaxSelectionIndex(); i++)
    if (model.isSelectedIndex(i)) System.out.print(i + " ");
System.out.println();

当你运行这段代码时,你会得到这样的输出:

Selected indices before insert: 3 4 5 
Changed range reported by event: 3-8
Selected indices after insert: 3 4 5 6 7 8 

所以最初的选择是 3-5,插入新索引时扩大到 3-8。但是已经选择了 3-5,所以真正的变化只有 6-8 那么为什么事件告诉我范围 3-8 已经改变了?当您将 insertIndexInterval 调用更改为此时,情况会更加困惑:

model.insertIndexInterval(3, 3, false);  

现在输出是这样的:

Selected indices before insert: 3 4 5 
Changed range reported by event: 5-8
Selected indices after insert: 3 4 5 6 7 8 

我不知道为什么报告的变化是 5-8。

此方法的 API 文档太短,无法理解那里发生了什么。特别是这个 before 参数对我来说是个谜,因为它从来没有对选择产生任何影响,但它似乎对事件以及引导和 anchor 索引有一些影响。

我什至无法为我的实现编写单元测试,因为我根本不知道预期的结果。

那么有人可以详细解释这个方法(尤其是 before 标志)在做什么,它对选择模型和 ListSelectionEvent 有什么副作用?

最佳答案

当新数据添加到数据模型时使用它(因此应该移动当前选择索引)。如果 2 表示“新添加和选择”,那么您的输出将是:

[0,0,0,1,1,1,0,0,0,0] == [3A,4,5L]
-> add after [0,0,0,1,2,2,2,1,1,0] == [3A,4,5,6,7,8L]
-> add before [0,0,0,2,2,2,1,1,1,0] == [3,4,5,6A,7,8L]

您在这里看到的是 DefaultListSelectionModel 的一个特性* - 在当前选择中添加索引,自动扩展选择。

例如,从选择索引 1 开始,然后在索引 3 处插入三行:

[1,0,0,0,0,0,0,0,0,0]
-> add after [1,0,0,0,0,0,0,0,0,0]

请注意,您的选择表示具有误导性,零实际上并不存在。打印选择状态的更好方法是:

private static void printSelection(ListSelectionModel model) {
    System.out.print("[");
    for (int i = model.getMinSelectionIndex(); i <= model.getMaxSelectionIndex(); i++) {
        if(i > model.getMinSelectionIndex()) {
            System.out.print(",");
        }
        if(model.isSelectedIndex(i)) {
            System.out.print(i);
            if(i == model.getAnchorSelectionIndex()) {
                System.out.print("A");
            }
            if(i == model.getLeadSelectionIndex()) {
                System.out.print("L");
            }
        }
    }
    System.out.println("]");
}

*) DefaultListSelectionModel#insertIndexInterval 的文档与接口(interface)不同,另请参见 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4700643http://java.net/jira/browse/SWINGX-272

关于java - ListSelectionModel.insertIndexInterval() 究竟在做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9566536/

相关文章:

java - 将滚动条添加到我的文本区域,但我的图像未显示

java - 在 JFrame 中显示 HTML 代码预览?

java - 为什么我的匿名类操作监听器无法退出 Java GUI?

java - 调用方法列表

java - Android Studio 与 DatatypeConverter 中 Base64 的使用

java - 作业中索引越界异常

java - 每次鼠标移动时获取鼠标坐标

java - itext 无法解析 bootstrap.css

java - 向 JScrollBar 添加滚动锁定按钮

java - 在几个JLabel中设置相同的gif作为图标