2012-03-05 2 views
3

ListSelectionModel의 자체 구현을 작성하려고하는데 현재는 insertIndexInterval을 구현하는 동안 멈추었습니다. 나는 Sun/Oracle의 DefautListSelectionModel 구현에서이 방법의 결과를 이해하지 못한다. 이 코드를 실행하면이 출력을 얻을 것이다ListSelectionModel.insertIndexInterval()은 정확히 무엇입니까?

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 범위가 변경되었다고 말하는 이유는 무엇입니까?

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 왜 아무 생각이에 insertIndexInterval 전화를 변경할 때 더욱 혼란 스럽다.

이 메소드의 API 문서는 너무 짧아서 현재 진행중인 작업을 이해할 수 없습니다. 특히이 before 매개 변수는 선택에 아무런 영향을 미치지 않으므로 미스터리이지만 이벤트 및 리드 및 앵커 인덱스에 영향을 미치는 것으로 보입니다.

단순히 예상 결과를 모르기 때문에 구현에 대한 단위 테스트를 작성할 수 없습니다.

누군가가이 방법 (특히 before 플래그)을 수행하고 있으며 선택 모델 및 ListSelectionEvent에 어떤 부작용이 있는지 자세히 설명 할 수 있습니까?

답변

4

이 그것은 사용되며, 하나의 dimension 또는 directional을 가지고있다. 이 수단은 '새로 추가하고 선택'보다 경우 출력은 다음과 같습니다

[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 예를 들어, 다음 인덱스 세에서 세 행 삽입과 : 선택 표현은 오해의 소지가

[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

+0

예, 0은 실제로 존재하지 않습니다. 그렇게 쉽게 비교할 수 있습니다. 그러나 나는 아직도 그것을 얻지 못한다. 이벤트는 데이터 모델이 아닌 선택 모델에 바인딩됩니다. 인덱스 3-5는 새로운 데이터를 삽입하기 전에 이미 선택되었으므로 (데이터 모델이 아닌 선택 모델에 대한) 유일한 실제 변경 사항은 6-8의 추가 선택 범위입니다. 그러나보고 된 변경 사항은 3-8 (이전 = true) 및 5-8 (이전 = false)입니다. 특히이 5-8은 지금까지 의미가 없습니다. – kayahr

+1

예, 3-8을 설명합니다 (1이 이동되었고 2가 추가 되었기 때문에)하지만 before = false를 사용할 때보고 된 5-8 범위를 어떻게 설명합니까? 3 상태로 시각화하면 세 개의 1은 이동하지 않고 세 개의 2는 5가 아닌 6의 인덱스에 삽입됩니다. – kayahr

+1

BTW : 인덱스 3 이후에 삽입 할 때 0001222110이어야하고 0001112220이 아닌가요? 문서에 "길이 인덱스 시작 전/후에 인덱스 삽입"이 나와 있습니다. – kayahr

-2

편집 나는 우리가 JList의 얘기를 바란다는하지 JTable에 대해, 다음 나머지는

SSCCE를 사용하여 코드를 명확하게 ListSelectionModel, ListeSelectionMode 및 관련 코드의 사용하여 문제를 domostrating하여 질문을 수정하시기 바랍니다 할 수있는 고지에 해결 방법은, noticeListener 새로운 데이터가 데이터 모델 (그래서 현재의 선택 인덱스를 이동해야합니다)에 추가 될 때

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(); 
      } 
     }); 
    } 
+1

귀하의 게시 된 코드와는 전혀 무관 참조 달라 문제. –

+0

@Walter Laan 맞습니다.하지만 왜 SelectionMode를 알지 못한 채 괴롭 히면, 답안에 설명하는 것처럼 더 간단 할 수 있습니다. 귀찮아서 편집하고 나머지는 답안의 주석에 나타납니다 .-) – mKorbel

+0

나는 ' 전체 코드 예제를 추가했습니다. 나는 그것이 내 문제를 충분히 보여주기를 바란다. – kayahr