我正在尝试编写自己的ListSelectionModel实现,但目前我在尝试实现insertIndexInterval时被卡住了。我不理解这种方法在Sun的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有什么副作用
发布于 2012-03-05 21:02:44
在将新数据添加到数据模型时使用它(因此应该移动当前选择索引)。如果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=4700643和http://java.net/jira/browse/SWINGX-272
发布于 2012-03-05 20:41:35
编辑我希望我们谈论的是JList,而不是JTable,那么rest就在通知中了
您能否使用此SSCCE并使用代码编辑您的问题,清楚地说明您与ListSelectionModel、ListeSelectionMode和相关代码的问题的解决方法,notice此Listener只有一个dimension或directional,
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();
}
});
}https://stackoverflow.com/questions/9566536
复制相似问题