2011-03-17 7 views

答えて

-1

これをテーブルに追加します。 rowClickedcolClickedの2つのint個のグローバルがあります。それにKeyListenerを追加し、あなたがイベントを登録するには、キーボードを使用しての話をする場合は、選択したセルを見つけなければならない

 

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

を行くために良いことがあります。次のコードを使用して、選択したセルを見つけることができます。実際には、セル選択モードに依存します。

 

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

すべてのSwingコンポーネントは、アクションを使用してキーストロークを処理します。 Enterキーのデフォルトのアクションは、セル選択を1行下に移動することです。この動作を変更する場合は、デフォルトのアクションをカスタムアクションに置き換える必要があります。

アクションを置き換える簡単な説明については、Key Bindingsを参照してください。

関連する問題