Swingset2 中没有排序数据列呀!

解决方案 »

  1.   

    这个是按字符顺序排序的,又时间的话帮我看看(也是JTable)
    http://expert.csdn.net/Expert/topic/2000/2000083.xml?temp=.1850702import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;public class Test extends JFrame{
    JTable table = new JTable(new Object[][]{
    {"applet","$.39","2002/07/02 9:12"},{"mango","$.49","2003/07/02 10:12"},
    {"papaya","$1.19","2002/07/02 9:12"},{"lemon","$.19","2001/07/02 10:12"},
    {"aaplet","$.39","2002/06/02 9:12"},{"aqngo","$.49","2002/07/03 9:12"},
    },
    new Object[]{"Item","Price/lb.0","Time"}); public Test(){
    final SortDecorator decorator = new SortDecorator(table.getModel());
    table.setModel(decorator);
    JTableHeader hdr = (JTableHeader)table.getTableHeader();
    hdr.addMouseListener(new MouseAdapter(){
    public void mouseClicked(MouseEvent e){
    TableColumnModel tcm = table.getColumnModel();
    int vc = tcm.getColumnIndexAtX(e.getX());
    int mc = table.convertColumnIndexToModel(vc);
    decorator.sort(mc);
    }
    });
    JFrame f = new JFrame("Tables");
    Container container = f.getContentPane();
    container.add(new JScrollPane(table),BorderLayout.CENTER);
    f.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    });
    f.setSize(300,250);
    f.show();
    }
    public static void main(String args[]){
    new Test();
    }
    }
    class SortDecorator implements TableModel,TableModelListener{ private boolean flag = false; private TableModel realModel;
    private int indexes[];
    public SortDecorator(TableModel model){
    if(model==null){
    System.out.println("null");
    }
    this.realModel = model;
    realModel.addTableModelListener(this);
    allocate();
    }
    public Object getValueAt(int row,int column){
    return realModel.getValueAt(indexes[row],column);
    }
    public void setValueAt(Object aValue,int row,int column){
    realModel.setValueAt(aValue,indexes[row],column);
    }
    public void tableChanged(TableModelEvent e){
    allocate();
    }
    public void sort(int column){
    int rowCount = getRowCount();
    for(int i=0;i<rowCount;i++){
    for(int j=i+1;j<rowCount;j++){
    if (compare(indexes[i],indexes[j],column)<0)
    swap(i,j);
    }
    }
    flag = !flag;
    }
    public void swap(int i,int j){
    int tmp = indexes[i];
    indexes[i] = indexes[j];
    indexes[j] = tmp;
    }
    public int compare(int i,int j,int column){
    Object io = realModel.getValueAt(i,column);
    Object jo = realModel.getValueAt(j,column);
    int c= jo.toString().compareTo(io.toString());
    if(!flag)
    return(c<0)?-1:((c>0)?1:0);
    else
    return(c>0)?-1:((c<0)?1:0);
    }
    private void allocate(){
    indexes = new int[getRowCount()];
    for(int i=0;i<indexes.length;i++){
    indexes[i] = i;
    }
    }
    public int getRowCount(){
    return realModel.getRowCount();
    }
    public int getColumnCount(){
    return realModel.getColumnCount();
    }
    public String getColumnName(int columnIndex){
    return realModel.getColumnName(columnIndex);
    }
    public Class getColumnClass(int columnIndex){
    return realModel.getColumnClass(columnIndex);
    }
    public boolean isCellEditable(int rowIndex,int columnIndex){
    return realModel.isCellEditable(rowIndex,columnIndex);
    }
    public void addTableModelListener(TableModelListener l){
    realModel.addTableModelListener(l);
    }
    public void removeTableModelListener(TableModelListener l){
    realModel.removeTableModelListener(l);
    }
    }
      

  2.   

    sun的swing tutorial就有例子啊。
    java profession programming上也有。
    import javax.swing.table.*;
    import java.util.*;
    public class SortedTableModel extends AbstractTableModel {
      protected TableModel sourceModel;
      protected int[] indexValues;  public SortedTableModel(TableModel model) {
        super();
        sourceModel = model;
      }  public int getRowCount() {
        return sourceModel.getRowCount();
      }   public int getColumnCount() {
        return sourceModel.getColumnCount();
      }   public Object getValueAt(int row, int column) {
        if (indexValues != null) {
          row = getSourceIndex(row);
        } 
        return sourceModel.getValueAt(row, column);
      }   public void setValueAt(Object value, int row, int column) {
        if (indexValues != null) {
          row = getSourceIndex(row);
        } 
        sourceModel.setValueAt(value, row, column);
      }   public boolean isCellEditable(int row, int column) {
        return sourceModel.isCellEditable(row, column);
      }   public String getColumnName(int column) {
        return sourceModel.getColumnName(column);
      }   public Class getColumnClass(int column) {
        return sourceModel.getColumnClass(column);
      }   public int getSourceIndex(int index) {
        if (indexValues != null) {
          return indexValues[index];
        } 
        return -1;
      }   public void sortRows(int column, boolean ascending) {
        SortedItemHolder holder;
        TreeSet sortedList = new TreeSet();
        int count = getRowCount();
        for (int i = 0; i < count; i++) {
          holder = new SortedItemHolder(sourceModel.getValueAt(i, column), i);
          sortedList.add(holder);
        } 
        indexValues = new int[count];
        Iterator iterator = sortedList.iterator();
        int index = (ascending ? 0 : count - 1);
        while (iterator.hasNext()) {
          holder = (SortedItemHolder)(iterator.next());
          indexValues[index] = holder.position;
          index += (ascending ? 1 : -1);
        } 
        refreshViews();
      }   public void clearSort() {
        indexValues = null;
        refreshViews();
      }   public void refreshViews() {
        fireTableDataChanged();
      }   class SortedItemHolder implements Comparable {    public final Object value;
        public final int position;    public SortedItemHolder(Object value, int position) {
          this.value = value;
          this.position = position;
        }    public int compareTo(Object parm) {
          SortedItemHolder holder = (SortedItemHolder)parm;
          Comparable comp = (Comparable)value;
          int result = comp.compareTo(holder.value);
          if (result == 0) {
            result = (position < holder.position) ? -1 : 1;
          } 
          return result;
        }     public int hashCode() {
          return position;
        }     public boolean equals(Object comp) {
          if (comp instanceof SortedItemHolder) {
            SortedItemHolder other = (SortedItemHolder)comp;
            if ((position == other.position) && (value == other.value)) {
              return true;
            } 
          } 
          return false;
        }   }}