2011-03-17 3 views

답변

-1

테이블에 추가하십시오. rowClickedcolClicked에 두 개의 int 개의 전역이 있어야합니다. 당신이 이벤트를 등록하는 키보드를 사용하여 말하는 경우

 

     table.addMouseListener(new MouseAdapter(){ 
      public void mouseReleased(MouseEvent e) 
      { 
       rowClicked = rowAtPoint(e.getPoint()); 
       colClicked = columnAtPoint(e.getPoint()); 
      } 

      public void mouseClicked(MouseEvent e) 
      { 
       rowClicked = rowAtPoint(e.getPoint()); 
       colClicked = columnAtPoint(e.getPoint()); 
      } 
     }); 
 

을 갈 수있을 경우, 당신은 다음에 KeyListener를 추가하고, 선택한 셀을 찾아야합니다. 다음 코드를 사용하여 선택한 셀을 찾을 수 있습니다. 셀 선택 모드에 따라 달라집니다.

 

public void getSelectedCells() 
    { 
     if (getColumnSelectionAllowed() && ! getRowSelectionAllowed()) 
     { 
      // Column selection is enabled 
      // Get the indices of the selected columns 
      int[] vColIndices = getSelectedColumns(); 
     } 
     else if (!getColumnSelectionAllowed() && getRowSelectionAllowed()) 
     { 
      // Row selection is enabled 
      // Get the indices of the selected rows 
      int[] rowIndices = getSelectedRows(); 
     } 
     else if (getCellSelectionEnabled()) 
     { 
      // Individual cell selection is enabled 

      // In SINGLE_SELECTION mode, the selected cell can be retrieved using 
      setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
      int rowIndex = getSelectedRow(); 
      int colIndex = getSelectedColumn(); 

      // In the other modes, the set of selected cells can be retrieved using 
      setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); 
      setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 

      // Get the min and max ranges of selected cells 
      int rowIndexStart = getSelectedRow(); 
      int rowIndexEnd = getSelectionModel().getMaxSelectionIndex(); 
      int colIndexStart = getSelectedColumn(); 
      int colIndexEnd = getColumnModel().getSelectionModel().getMaxSelectionIndex(); 

      // Check each cell in the range 
      for (int r = rowIndexStart; r < = rowIndexEnd; r++) 
      { 
       for (int c = colIndexStart; c < = colIndexEnd; c++) 
       { 
        if (isCellSelected(r, c)) 
        { 
         // cell is selected 
        } 
       } 
      } 
     } 
    } 
 
+0

@User에서 KeyListener는 셀의 이벤트를 캡처하지 않습니다. – jzd

+0

청취자의 경우 2 * -1, 선택한 셀을 수집하는 경우 +1 – kleopatra

1

모든 스윙 구성 요소는 동작을 사용하여 키 스트로크를 처리합니다. Enter 키의 기본 동작은 셀 선택 항목을 한 행 아래로 이동하는 것입니다. 이 동작을 변경하려면 기본 동작을 사용자 지정 동작으로 바꿔야합니다.

조치를 대체하는 방법에 대한 간단한 설명은 Key Bindings을 확인하십시오.

관련 문제