2012-01-03 107 views
0

我遇到了JTable上彈出菜單的問題,以及這個JTable允許多次間隔選擇的問題。 我將詳細解釋我的情況,希望儘可能清楚。多重選擇和JPopupMenu點擊右鍵

我有一個基本的數據類,讓我們叫它Item,一個字符串id(名稱)和兩個布爾字段,onlineactive(與相對獲得者)。
JTable背後的想法是,對於數據集中的每個項目,它將在第一列中顯示其名稱,並在第二列中顯示其狀態,其中狀態'我的意思是,它將顯示「ACTIVE/NOT活動「,如果項目是聯機的,否則它將顯示」脫機「。 我已經實現了一個TableModel來完成這個工作並且工作。

我還希望,當用戶右鍵單擊某一行時,會出現一個彈出窗口(如果選擇的項目在線),允許激活/取消激活該項目,具體取決於其狀態。 只要選擇模型是單選,此功能就完美了,但當我將其更改爲多選區間時,我無法再正常工作。

我想要的行爲是,在右鍵單擊時,彈出窗口出現在執行點擊的位置,行被添加到選區並高亮顯示,並且所有先前選定的行保持選定狀態!這我無法做到!

這裏是我在的MouseListener代碼:

tblModel.addMouseListener(new MouseAdapter() { 
    void showPopup(MouseEvent e){ 
      int r = tblModel.rowAtPoint(e.getPoint()); 
      if (r >= 0 && r < tblModel.getRowCount()) { 
        //tblModel.setRowSelectionInterval(r, r); 
       } else { 
        tblModel.clearSelection(); 
       } 
      int[] viewRowIndexes = tblModel.getSelectedRows();   
      int rowViewIndex = tblModel.getSelectedRow(); 

      if (rowViewIndex < 0) 
       return; 

      int rowModelIndex = tblModel.convertRowIndexToModel(rowViewIndex); 
      if (e.isPopupTrigger() && e.getComponent() instanceof JTable) { 
       Action changeActiveAction; 
       Action changeInactiveAction; 
       List<String> actives = new ArrayList<String>(); 
       List<String> inactives = new ArrayList<String>(); 
       DefaultListSelectionModel selectionModel = (DefaultListSelectionModel) tblModel.getSelectionModel(); 

       for (int viewRowIndex : viewRowIndexes) { 
        int modelRowIndex = tblModel.convertRowIndexToModel(viewRowIndex); 
        if (selectionModel.isSelectedIndex(viewRowIndex)) { 
         boolean online = ((MyTableModel) tblModel.getModel()).isItemOnline(modelRowIndex); 
         if (!online) 
          continue; 

         boolean active = ((MyTableModel) tblModel.getModel()).isItemActive(modelRowIndex); 
         String idItem = (String) ((MyTableModel) tblModel.getModel()).getValueAt(modelRowIndex,0); 
         if (active) { 
          actives.add(idItem); 
         } else { 
          inactives.add(idItem); 
         } 
        } 
       } 

       if (actives.size() > 0 || inactives.size() > 0) { 
        popup = new JPopupMenu(); 
        if (actives.size() > 0) { 
         changeActiveAction = new ChangeAction("Deactivate ACTIVE Items","This will deactivate all the selected ACTIVE items",actives, false); 
         popup.add(new JMenuItem(changeActiveAction)); 
        } 
        if (inactives.size() > 0) { 
         changeInactiveAction = new ChangeAction("Activate INACTIVE Items","This will activate all the selected INACTIVE items",inactives, true); 
         popup.add(new JMenuItem(changeInactiveAction)); 
        } 
        popup.show(e.getComponent(), e.getX(),e.getY()); 
       } 
      } 
    } 
    @Override 
    public void mousePressed(MouseEvent e) { 
     showPopup(e); 
    } 

    @Override 
    public void mouseReleased(MouseEvent e) { 
     showPopup(e); 
    } 
}; 

的行爲是正確的功能,但行的選擇是行不通的。 已經註釋行

//tblModel.setRowSelectionInterval(r, r); 

當我連續單擊鼠標右鍵,彈出的出現,但是卻忽略了在我點擊了排。

在另一方面,如果未加註釋,該行會僅選擇點擊的行,失去了選擇的所有的休息....

我在很長的帖子對不起,我不知道如何解釋我的問題,而不提供我的情況的所有細節.... 希望這是一件小事,你可以告訴我如何修復/改變它。

預先感謝您。答案

+1

1)爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/)。 2)*「所有以前選擇的行」*請不要對我們SHOUT。 3)*「對於長篇帖子感到抱歉」*海事組織不是特別長。 – 2012-01-03 15:30:26

+0

1)我已經簡化了我的代碼並使其「可編譯」,我應該多發佈3個課程......在我看來,這似乎是一種矯枉過正。 2)你是對的,編輯了。 3)有用的知識:) – mdm 2012-01-03 15:40:38

回答

3

一個部分是:

if (tblModel.isSelectedIndex(r)) { 
    tblModel.removeSelectionInterval(r, r); 
} else { 
    tblModel.addSelectionInterval(r, r); 
} 
+0

+1使用ListSelectionModel。 – camickr 2012-01-03 16:23:21