ListSelectionModel.insertIndexInterval()到底在做什么?

时间:2012-03-05 12:20:34

标签: 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参数对我来说是一个谜,因为它对选择没有任何影响,但它似乎对事件以及引导和锚索引有一些影响。

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

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

2 个答案:

答案 0 :(得分:4)

将新数据添加到数据模型时使用(因此应移动当前选择索引)。如果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的文档与界面不同,另请参阅http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4700643http://java.net/jira/browse/SWINGX-272

答案 1 :(得分:-2)

编辑我希望我们谈论JList,而不是关于JTable,然后休息就在通知中

是否可以使用此SSCCE并使用代码编辑您的问题,并使用ListSelectionModelListeSelectionMode及相关代码的解决方法清楚地解决您的问题,noticeListener只有一个dimensiondirectional

import java.awt.Component;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import javax.swing.*;

public class Ctrl_Down_JList {

    private static void createAndShowUI() {
        String[] items = {"Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"};
        JList myJList = new JList(items) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void processMouseEvent(MouseEvent e) {
                int modifiers = e.getModifiers() | InputEvent.CTRL_MASK;
                // change the modifiers to believe that control key is down
                int modifiersEx = e.getModifiersEx() | InputEvent.CTRL_MASK;
                // can I use this anywhere?  I don't see how to change the modifiersEx of the MouseEvent
                MouseEvent myME = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(), modifiers, e.getX(),
                        e.getY(), e.getXOnScreen(), e.getYOnScreen(), e.getClickCount(), e.isPopupTrigger(), e.getButton());
                super.processMouseEvent(myME);
            }
        };
        JFrame frame = new JFrame("Ctrl_Down_JList");
        frame.getContentPane().add(new JScrollPane(myJList));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowUI();
            }
        });
    }