jtable 排序问题 怎么样可以实现点击一个jtable的表头,就可以对这一列进行排序,比如我要让我输入的人名排序,怎么以英文字母的顺序排序雅 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://gceclub.sun.com.cn/yuanchuang/2004_Q4/swing.html public class MyTable extends JPanel{ public MyTable() { JTable table = new JTable(); Object[][] obj=new Object[][]{{"asdas","12321","3as3","eqw22"}, {"das2","asd","qweqw","as23"},{"aere","r32re","ewfse","werew3"}}; DefaultTableModel model=new DefaultTableModel(obj,new Object[]{"a","b","c","d"}); table.setModel(model); JScrollPane scrPane = new JScrollPane(table); JButton addBtn = new JButton("新增行"); addBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); JPanel btnPane = new JPanel(); btnPane.add(addBtn); add(scrPane); add("South", btnPane); table.setAutoCreateColumnsFromModel(false); sortAllRowsBy(model, 0, false); } public void sortAllRowsBy(DefaultTableModel model, int colIndex, boolean ascending) { Vector data = model.getDataVector(); Collections.sort(data, new ColumnSorter(colIndex, ascending)); model.fireTableStructureChanged(); } public static void main(String[] args)throws Exception{ JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.getContentPane().add(new MyTable()); frame.pack(); frame.setVisible(true); }}class ColumnSorter implements Comparator { int colIndex; boolean ascending; ColumnSorter(int colIndex, boolean ascending) { this.colIndex = colIndex; this.ascending = ascending; } public int compare(Object a, Object b) { Vector v1 = (Vector)a; Vector v2 = (Vector)b; Object o1 = v1.get(colIndex); Object o2 = v2.get(colIndex); if (o1 instanceof String && ((String)o1).length() == 0) { o1 = null; } if (o2 instanceof String && ((String)o2).length() == 0) { o2 = null; } if (o1 == null && o2 == null) { return 0; } else if (o1 == null) { return 1; } else if (o2 == null) { return -1; } else if (o1 instanceof Comparable) { if (ascending) { return ((Comparable)o1).compareTo(o2); } else { return ((Comparable)o2).compareTo(o1); } } else { if (ascending) { return o1.toString().compareTo(o2.toString()); } else { return o2.toString().compareTo(o1.toString()); } } }} sortAllRowsBy(model, 0, false);这一行的0和false就是指定排序column和顺序或者逆序你加入到Header的事件中就可以了 请问inner class的一点小问题? 输出年月日的程序! ArrayList.toArray如何返回指定类型的数组 JTree中怎么改变节点的字体颜色? 文件转换问题,谢了!!! 编一个简单程序,以确定给定字符是否为中文字符 请问那些很标准的java api 文档是用什么做的? GC会回收常量池里面的内容吗? 关于正则表达式的概念性问题。。。。。头疼死了,,求大神详解 LinkedList的对象不能访问其方法,什么回事 计算机网络问题 渴望前辈指点指点
JTable table = new JTable();
Object[][] obj=new Object[][]{{"asdas","12321","3as3","eqw22"},
{"das2","asd","qweqw","as23"},{"aere","r32re","ewfse","werew3"}};
DefaultTableModel model=new DefaultTableModel(obj,new Object[]{"a","b","c","d"});
table.setModel(model);
JScrollPane scrPane = new JScrollPane(table);
JButton addBtn = new JButton("新增行");
addBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
JPanel btnPane = new JPanel();
btnPane.add(addBtn); add(scrPane);
add("South", btnPane);
table.setAutoCreateColumnsFromModel(false);
sortAllRowsBy(model, 0, false);
} public void sortAllRowsBy(DefaultTableModel model, int colIndex, boolean ascending) {
Vector data = model.getDataVector();
Collections.sort(data, new ColumnSorter(colIndex, ascending));
model.fireTableStructureChanged();
}
public static void main(String[] args)throws Exception{
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyTable());
frame.pack();
frame.setVisible(true);
}
}class ColumnSorter implements Comparator {
int colIndex;
boolean ascending;
ColumnSorter(int colIndex, boolean ascending) {
this.colIndex = colIndex;
this.ascending = ascending;
}
public int compare(Object a, Object b) {
Vector v1 = (Vector)a;
Vector v2 = (Vector)b;
Object o1 = v1.get(colIndex);
Object o2 = v2.get(colIndex); if (o1 instanceof String && ((String)o1).length() == 0) {
o1 = null;
}
if (o2 instanceof String && ((String)o2).length() == 0) {
o2 = null;
} if (o1 == null && o2 == null) {
return 0;
} else if (o1 == null) {
return 1;
} else if (o2 == null) {
return -1;
} else if (o1 instanceof Comparable) {
if (ascending) {
return ((Comparable)o1).compareTo(o2);
} else {
return ((Comparable)o2).compareTo(o1);
}
} else {
if (ascending) {
return o1.toString().compareTo(o2.toString());
} else {
return o2.toString().compareTo(o1.toString());
}
}
}
}
这一行的0和false就是指定排序column和顺序或者逆序你加入到Header的事件中就可以了