用表格显示数据库中的数据,如何能双击表头能对列进行排序并写入数据库
解决方案 »
- ITAT复赛试题答案(帮帮忙吧!!!
- JTable中的checkbox问题
- [求助]这个程序有点问题(关于事件处理|含代码)
- synth swing jComboBox 及最大化最小化 xml
- 推荐一个类似excel界面的开源Grid 控件(java版本)
- 学习java中遇到的若干问题解决后的疑惑?很重要也绝对对您有用噢!!!
- 大家谁有完整的win2000下配置tomcat3.2.4作web服务器的文档,谢谢!
- 请问??????????????
- 概念题
- 请教大神Android的xml文件具体打开方式
- 我自己做了个画图程序,画出图形后,怎样增加对鼠标的响应?画板是Jpanel,图形用Image
- java程序怎样控制让他定时执行?
写入数据库就不太明白你什么意思了
应该是从数据库中读出来的数据吧下面有个排序的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;
}
}
}用这个就可以对表格的列进行排序了
排序只是一个view,视图而已,