利用MouseAdapter来实现自排序的表格

利用MouseAdapter来实现自排序的表格

本文参考了 http://www.java2s.com/Code/Java/Swing-JFC/TableSortTest.htm的做法。主要处理是取得用户点击的列,得到按此列排序的新数组,删除原有元素,再把新数组加入进表格;如果已经排序,则进行逆序处理。处理完毕后,用户点击表头即可实现排序和逆序。


首先请增加这样三个类:
SortTableMouseAdaptor类:用以排序的关键类。
public   class  SortTableMouseAdaptor  extends  MouseAdapter {
    
private JTable table;
    
    
public SortTableMouseAdaptor(JTable table){
        
this.table=table;
    }

    
    
public void mouseClicked(MouseEvent event) // check for double
        /**//*
         * if (event.getClickCount() < 2) return;
         
*/


        
// find column of click and
        int tableColumn = table.columnAtPoint(event.getPoint());

        
// translate to table model index and sort
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

        
int rowCount = tableModel.getRowCount();
        
int cloumnCount = tableModel.getColumnCount();

        SortTableRow[] tableRowArr 
= new SortTableRow[rowCount];

        
int j;
        
for (int i = 0; i < rowCount; i++{
            tableRowArr[i] 
= new SortTableRow(cloumnCount, tableColumn);
            
            
for (j = 0; j < cloumnCount; j++{
                tableRowArr[i].setArrValue(j, tableModel.getValueAt(i,j));
            }

        }

        
        SortCompareColum compareColum
=new SortCompareColum(rowCount);
        
for (int i = 0; i < rowCount; i++{
            compareColum.setArrValue(i, tableModel.getValueAt(i,tableColumn));
        }

        
        tableModel.setRowCount(
0);
        Arrays.sort(tableRowArr);
        
        
if(compareColum.isSorted()){
            List ls
=Arrays.asList(tableRowArr);
            Collections.reverse(ls);
            
            
for (int i = 0; i < ls.size(); i++{
                SortTableRow tableRow
=(SortTableRow)ls.get(i);
                
                tableModel.addRow(tableRow.getArr());
            }

        }

        
else{                
            
for (int i = 0; i < tableRowArr.length; i++{
                tableModel.addRow(tableRowArr[i].getArr());
            }

        }


        
        table.invalidate();
    }

}

SortCompareColum类,可以作为SortTableMouseAdaptor类的内部类使用:
public   class  SortCompareColum {
    
private Object[] arr;
    
    
public SortCompareColum(int arrLength){
        arr 
= new Object[arrLength];
    }

    
    
public void setArrValue(int i, Object value) {
        arr[i] 
= value;
    }

    
    
public boolean isSorted(){
        
boolean retval=true;
        
        
for(int i=0;i<arr.length-1;i++){
            
            
try{
                
int op1 =Integer.valueOf((String) arr[i]);
                
int op2 = Integer.valueOf((String) arr[i+1]);
                
                retval
=retval && (op1-op2>0?false:true);
            }

            
catch(Exception e){
                String op1 
= (String) arr[i];
                String op2 
= (String) arr[i+1];
                
                retval
=retval && (op1.compareTo(op2)>0?false:true);
            }

        }

        
        
return retval;
    }

}


SortTableRow类,可以作为SortTableMouseAdaptor类的内部类使用:
public   class  SortTableRow  implements  Comparable  {
    
private Object[] arr;

    
public int compareColumnIndex;

    
public SortTableRow(int arrLength, int compareColumnIndex) {
        arr 
= new Object[arrLength];
        ;
        
this.compareColumnIndex = compareColumnIndex;
    }


    
public void setArrValue(int i, Object value) {
        arr[i] 
= value;
    }


    
public int compareTo(Object other) {
        SortTableRow another 
= (SortTableRow) other;

        
try{            
            String op1 
= (String) this.getCompareObj();
            String op2 
= (String) another.getCompareObj();
            
int op3=Integer.valueOf(op1);
            
int op4=Integer.valueOf(op2);
            
            
return op3-op4;
        }

        
catch(Exception ex){
            String op1 
= (String) this.getCompareObj();
            String op2 
= (String) another.getCompareObj();
            
return op1.compareTo(op2);
        }
        
    }

    
    
public Object[] getArr() {
        
return arr;
    }


    
public Object getCompareObj() {
        
return arr[compareColumnIndex];
    }

}


使用时对需要排序的表格进行如下处理就行了,很简单方便:
table.getTableHeader().addMouseListener( new  SortTableMouseAdaptor(table));

三个类打包下载在此,欢迎试用:
http://www.blogjava.net/Files/sitinspring/sortTable20070712105832.rar

你可能感兴趣的:(利用MouseAdapter来实现自排序的表格)