用表格显示数据库中的数据,如何能双击表头能对列进行排序并写入数据库

解决方案 »

  1.   

    排序我有个例子
    写入数据库就不太明白你什么意思了
    应该是从数据库中读出来的数据吧下面有个排序的tablemodel,和你自己写的tablemodel
    一起用就可以了
    /**
     * This table model takes an existing table model and produces a new model that
     * sorts the rows so that the entries in a particular column are sorted.
     */class SortFilterModel extends AbstractTableModel {
            /**
             * Constructs a sort filter model.
             *
             * @param m
             *            the table model to filter
             */
            private TableModel model;        private int sortColumn;        private Row[] rows;        public SortFilterModel(TableModel m) {
                    model = m;
                    rows = new Row[model.getRowCount()];
                    for (int i = 0; i < rows.length; i++) {
                            rows[i] = new Row();
                            rows[i].index = i;
                    }
            }        /**
             * Sorts the rows.
             *
             * @param c
             *            the column that should become sorted
             */
            public void sort(int c) {
                    sortColumn = c;
                    Arrays.sort(rows);
                    fireTableDataChanged();
            }        // Compute the moved row for the three methods that access
            // model elements        public Object getValueAt(int r, int c) {
                    return model.getValueAt(rows[r].index, c);
            }        /*
             * Can't edit the value now,but can be added "Rename" later
             */
            public boolean isCellEditable(int r, int c) {
                    return false;
            }        public void setValueAt(Object aValue, int r, int c) {
                    model.setValueAt(aValue, rows[r].index, c);
            }        // delegate all remaining methods to the model        public int getRowCount() {
                    return model.getRowCount();
            }        public int getColumnCount() {
                    return model.getColumnCount();
            }        public String getColumnName(int c) {
                    return model.getColumnName(c);
            }        public Class getColumnClass(int c) {
                    return model.getColumnClass(c);
            }        /**
             * This inner class holds the index of the model row Rows are compared by
             * looking at the model row entries in the sort column.
             */
            private class Row implements Comparable {
                    public int index;                public int compareTo(Object other) {
                            Row otherRow = (Row) other;
                            Object a = model.getValueAt(index, sortColumn);
                            Object b = model.getValueAt(otherRow.index, sortColumn);
                            if (a instanceof Comparable)
                                    return ((Comparable) a).compareTo(b);
                            else
                                    return a.toString().compareTo(b.toString());                        // return index - otherRow.index;
                    }
            }
    }用这个就可以对表格的列进行排序了
      

  2.   

    排序简单,但是为什么要写入数据库,在数据库中存储,没有排序的概念
    排序只是一个view,视图而已,