繁体   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