首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListSelectionModel.insertIndexInterval()到底在做什么?

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

Stack Overflow用户
提问于 2012-03-05 20:20:34
回答 2查看 676关注 0票数 3

我正在尝试编写自己的ListSelectionModel实现,但目前我在尝试实现insertIndexInterval时被卡住了。我不理解这种方法在Sun的DefautListSelectionModel实现中的结果。下面是一个示例:

代码语言:javascript
复制
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();

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

代码语言:javascript
复制
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调用更改为以下代码时,它会更加令人困惑:

代码语言:javascript
复制
model.insertIndexInterval(3, 3, false);  

现在输出是这样的:

代码语言:javascript
复制
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有什么副作用

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-05 21:02:44

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

代码语言:javascript
复制
[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处插入三行:

代码语言:javascript
复制
[1,0,0,0,0,0,0,0,0,0]
-> add after [1,0,0,0,0,0,0,0,0,0]

请注意,您的选择表示是误导性的,零并不在那里。打印选择状态的更好方法是:

代码语言:javascript
复制
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

票数 4
EN

Stack Overflow用户

发布于 2012-03-05 20:41:35

编辑我希望我们谈论的是JList,而不是JTable,那么rest就在通知中了

您能否使用此SSCCE并使用代码编辑您的问题,清楚地说明您与ListSelectionModelListeSelectionMode和相关代码的问题的解决方法,noticeListener只有一个dimensiondirectional

代码语言:javascript
复制
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();
            }
        });
    }
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9566536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档