簡體   English   中英

如何通過在 java 中按下鼠標並拖動 jtable 來 select 幾個連續的列

[英]how to select a few continuous columns by pressing mouse and drag in jtable in java

我可以通過鼠標單擊列 header 來選擇一列: 在此處輸入圖像描述

但我想要的是按下並拖動鼠標到 select jtable 中的一些連續列,看起來就像我們在 excel 中做的一樣。 我不知道在 jtable 中制作它,任何人都可以發布一些示例代碼來做,非常感謝提前! 在此處輸入圖像描述

我不分叉,現在使用“鼠標拖動”對我來說似乎很困難,所以我使用鼠標單擊瞄准列 header 到 select ZC9FA155EA3AB5D652BD3FF5E397267C3 中的連續列下面的示例代碼:

......
public int columnIndex=0 ;  // for save column index
public SumDetail(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
  sumTB.getTableHeader().addMouseListener(getTableHeaderMouseAdapter(sumTB));  
 
}
private void formWindowOpened(java.awt.event.WindowEvent evt) {
......
columnIndex=tableModel.getRowCount()-1; 

}
.....
// for clicking on column header to select column
protected MouseAdapter getTableHeaderMouseAdapter(final JTable table) {
return new MouseAdapter() { 
    @Override
    public void mouseClicked(MouseEvent e) {  
         
           int c = table.columnAtPoint(e.getPoint());
           
           if(columnIndex>c){
               table.clearSelection();
              table.setColumnSelectionInterval(c, c);  
           }else{
                 table.setColumnSelectionInterval(columnIndex, c);
           }
                
      
             
          
            if (table.getRowCount() > 0) {
                table.setRowSelectionInterval(0, table.getRowCount() - 1);
                
            }
            
       
            columnIndex=c;
 
            
            if (table.isEditing()) {
                
                table.getCellEditor().stopCellEditing();
                
            }
           
    

    }
};
}

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM