Jtable排序

http://topic.csdn.net/u/20080110/17/17a6193c-e848-415b-adcc-5cd38c71facc.html

 

/*
* ColumnModelTest.java
*
* Created on 2008-1-11, 下午08:30:11
*/
package com.enrising.table;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

public class ColumnModelTest extends JPanel {
   
public ColumnModelTest() {
       
super(new BorderLayout());
        JTable table
= new JTable(new MyTableModel());
        add(
new JScrollPane(table), BorderLayout.CENTER); TableSorter ts = new TableSorter(table.getModel(), table.getTableHeader());
        table.setModel(ts);
    }

   
public static void createAndShowGUI() {
        JFrame f
= new JFrame("ColumnModelTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(
new ColumnModelTest(), BorderLayout.CENTER);
        f.pack();
        f.setVisible(
true);
    }

   
public static void main(String args[]) {
        javax.swing.SwingUtilities.invokeLater(
new Runnable() {

            @Override
           
public void run() {
                createAndShowGUI();
            }
        });

    }

   
public static class MyTableModel implements TableModel {

        Object[][] p
= {
                {
                       
new Boolean(false), "人事部", "12311", "无名", "", "19920103", "本科", "物理"
                }, {
                       
new Boolean(false), "人事部", "12312", "无名", "", "19920103", "本科", "物理"
                }, {
                       
new Boolean(false), "人事部", "12313", "无名", "", "19920103", "本科", "物理"
                }, {
                       
new Boolean(false), "人事部", "12314", "无名", "", "19920103", "本科", "物理"
                }
        };
        String[] n
= {
               
"选择", "部门", "教师编码", "姓名", "性别", "生日", "最终学历", "研究领域"
        };

       
public int getColumnCount() {
           
return n.length;
        }

       
public int getRowCount() {
           
return p.length;
        }

       
public String getColumnName(int col) {
           
return n[col];
        }

       
public Object getValueAt(int row, int col) {
           
return p[row][col];
        }

       
public Class getColumnClass(int c) {
           
return getValueAt(0, c).getClass();
        }

       
public boolean isCellEditable(int rowIndex, int columnIndex) {
           
return true;
        }

       
public void setValueAt(Object value, int row, int col) {
            p[row][col]
= value;
            fireTableCellUpdated(row, col);
        }

       
private void fireTableCellUpdated(int row, int col) {
           
// TODO Auto-generated method stub

        }

       
public void addTableModelListener(TableModelListener l) {

        }

       
public void removeTableModelListener(TableModelListener l) {

        }
    }

}

你可能感兴趣的:(html,.net)