這兩天公司讓做一個Jtable表格的排序,首先按A列排序,在A列相等時按B列排序,B列相等時按C列排序,ABC三列可以任意指定,最多分三列,這樣的一個需求。由於我是大神,所以必須做了出來。ok,不自戀了,先上效果圖,然後上代碼,最後上項目。這也將是我以後的一貫風格,懂得分享才能走的更高!
簡單描述一下,瞅准某列,第一次點擊升序,第二次點擊降序,第三次點擊取消排序。圖為先按密碼升序,然後按用戶名升序,最後按年齡降序。 一、 准備工作,及整體思路 我的環境,jdk1.6,jdk1.7,jdk1.8均測試過。公司用的是1.6,本人上傳此博客的環境為1.8。IDE 工具Eclipse Jee Neon,TableModel,List<T>。 思路:新建frame,new Jtable(我用的是tableModel創建的方式),設置Jtable樣式,給Jtable綁定表頭排序事件,給Jtable綁定表頭圖標事件。在表頭排序事件中將要排序列名傳遞給排序工具類(此工具類主要作用是維護哪幾列用來排序,別著急,下面有代碼),然後按此列進行排序,實現一個排序用的comparator類(自定義排序,當做int來排序,String排序,Long排序等)。 二、代碼 1. 主類(new frame)1 package com.test.order; 2 3 import java.awt.Dimension; 4 5 public class TableOrder3Test { 6 // private static int width = 600; 7 // private static int height = 600; 8 9 public static void main(String[] args) { 10 displayTable(); 11 12 } 13 14 public static void displayTable(){ 15 SwingUtilities.invokeLater(new Runnable() { 16 public void run() { 17 // 設置程序界面外觀 18 try { 19 UIManager 20 .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 21 } catch (Exception localException1) { 22 try { 23 UIManager.setLookAndFeel(UIManager 24 .getSystemLookAndFeelClassName()); 25 } catch (Exception localException2) { 26 localException2.printStackTrace(); 27 } 28 } 29 30 JFrame tableFrame = new JFrame("表格排序"); 31 32 TableOrder3Table order3Talbe = new TableOrder3Table(); 33 34 tableFrame.setContentPane(new JScrollPane(order3Talbe.initUI())); 35 36 Toolkit kit = Toolkit.getDefaultToolkit();//獲得窗口超類 37 Dimension dim = kit.getScreenSize();//獲得當前屏幕 38 // tableFrame.setLocation(((int)dim.getWidth()-width)/2, 39 // (int)(dim.getHeight()-height)/2);//設置frame距離左和上的偏移量,即窗口顯示的位置 40 // tableFrame.setLocationRelativeTo(null); 41 tableFrame.setSize(dim);//調整屏幕大小 42 // tableFrame.setSize(200, 200);//調整屏幕大小 43 tableFrame.setResizable(true);//是否可調整大小 44 tableFrame.setVisible(true);//是否顯示 45 tableFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//按叉之後執行哪個操作 46 } 47 }); 48 } 49 }
2. 關於Jtable類的代碼(下面的3,4,5,6,7中的類都將在此類中出現,注意它們在此類中的角色)
1 package com.test.order; 2 3 import java.awt.Component; 4 5 public class TableOrder3Table{ 6 7 private TableOrder3TableMode tabelMode; 8 private JTable order3Table; 9 private List<User> userList; 10 private TableOrderSorter tableOrderSorterUtil; 11 12 public JTable initUI(){ 13 14 getData(); 15 tabelMode = new TableOrder3TableMode(userList);//獲取model 16 order3Table = new JTable(); 17 order3Table.setModel(tabelMode);//設置model 18 addHeaderEvent();//添加對表頭排序事件 19 setSorterIconToTableHeader(order3Table.getTableHeader());//添加表頭排序圖標 20 return order3Table; 21 } 22 23 /** 24 * 添加對表頭排序事件 25 */ 26 public void addHeaderEvent(){ 27 order3Table.getTableHeader().addMouseListener(new MouseAdapter() { 28 @Override 29 public void mouseClicked(MouseEvent evt) { 30 if(evt.getButton() == MouseEvent.BUTTON1 && evt.getClickCount() == 1){ //判斷左鍵右鍵,點擊了幾次 31 int selectedColumn = order3Table.getTableHeader().columnAtPoint(evt.getPoint());//得到該鼠標點擊列的column,從0開始 32 String sortColumn = tabelMode.getColumnObjectName(selectedColumn);//獲得排序列名 33 SorterColumnGroupUtil.setColumn(sortColumn);//設置排序列,SorterColumnGroupUtil類便是用來維護排序列的工具類 34 userSort();//表格列排序 35 } 36 } 37 }); 38 } 39 40 /** 41 * 為表格表頭添加排序圖標 42 * @param tableHeader 43 */ 44 @SuppressWarnings("serial") 45 public void setSorterIconToTableHeader(JTableHeader tableHeader) { 46 tableHeader.setDefaultRenderer(new DefaultTableCellRenderer(){//添加tableheader渲染器 47 48 private Font TABLE_FONT = new Font("宋體", Font.PLAIN, 15); 49 @Override 50 public Component getTableCellRendererComponent(JTable table, 51 Object value, boolean isSelected, boolean hasFocus, int row, int column) { 52 JButton btn = new TableHeaderButton();//JideTool.newJideButton(); 53 btn.setText(value == null ? "" : value.toString()); 54 btn.setFont(TABLE_FONT); 55 String sortColumnName =""; 56 sortColumnName = tabelMode.getColumnObjectName(column); 57 if(sortColumnName.equals(SorterColumnGroupUtil.getColumn1())){ 58 if (Constant.ASC.equals(SorterColumnGroupUtil.getColumn1Asc())) { 59 btn.setIcon(getIconResource("arr-up1.png")); 60 } else if (Constant.DESC.equals(SorterColumnGroupUtil.getColumn1Asc())){ 61 btn.setIcon(getIconResource("arr-down1.png")); 62 } 63 }else if(sortColumnName.equals(SorterColumnGroupUtil.getColumn2())){ 64 if (Constant.ASC.equals(SorterColumnGroupUtil.getColumn2Asc())) { 65 btn.setIcon(getIconResource("arr-up2.png")); 66 } else if (Constant.DESC.equals(SorterColumnGroupUtil.getColumn2Asc())){ 67 btn.setIcon(getIconResource("arr-down2.png")); 68 } 69 }else if(sortColumnName.equals(SorterColumnGroupUtil.getColumn3())){ 70 if (Constant.ASC.equals(SorterColumnGroupUtil.getColumn3Asc())) { 71 btn.setIcon(getIconResource("arr-up3.png")); 72 } else if (Constant.DESC.equals(SorterColumnGroupUtil.getColumn3Asc())){ 73 btn.setIcon(getIconResource("arr-down3.png")); 74 } 75 } 76 return btn; 77 } 78 }); 79 } 80 81 /** 82 * 稍稍封裝了獲得icon的過程,默認搜索 83 */ 84 public static ImageIcon getIconResource(String iconName) 85 { 86 return new ImageIcon("icon/"+iconName); 87 } 88 89 /** 90 * 表格列排序 91 */ 92 public void userSort() { 93 Collections.sort(tabelMode.getList(), getOrderTableSorter());//將要排序的數據及自定義的排序類傳給Collections 94 order3Table.updateUI(); 95 } 96 97 /** 98 * 獲得排序類接口 99 * @return 100 */ 101 public TableOrderSorter getOrderTableSorter(){ 102 if(tableOrderSorterUtil == null){ 103 tableOrderSorterUtil = new TableOrderSorter(); 104 } 105 return tableOrderSorterUtil; 106 } 107 108 /** 109 * 設置表格樣式 110 */ 111 public void setStyle(){ 112 order3Table.getColumnModel().getColumn(2).setPreferredWidth(10); 113 order3Table.setRowHeight(25); 114 } 115 116 /** 117 * 添加對表格內容的事件 118 */ 119 public void addEvent(){ 120 order3Table.addMouseListener(new java.awt.event.MouseAdapter(){ 121 public void mouseClicked(MouseEvent e) {//僅當鼠標單擊時響應 122 //得到選中的行列的索引值 123 int r= order3Table.getSelectedRow(); 124 int c= order3Table.getSelectedColumn(); 125 //得到選中的單元格的值,表格中都是字符串 126 Object value= order3Table.getValueAt(r, c); 127 String info=r+"行"+c+"列值 : "+value.toString(); 128 javax.swing.JOptionPane.showMessageDialog(null,info); 129 } 130 }); 131 } 132 133 /** 134 * 獲取模擬數據 135 */ 136 public void getData(){ 137 userList = new ArrayList<User>(); 138 User user1 = new User(1, "大元帥", "哈", 18, "ddd", "eee"); 139 User user2 = new User(11, "帥帥", "bbb", 18, "drdfrt", "aba"); 140 User user3 = new User(10, "阿哥好帥", "dsd", 18, "dnca", "aba"); 141 User user4 = new User(2, "巴拉拉", "sdf", 18, "wer", "aba"); 142 User user5 = new User(3, "小魔仙", "abc", 18, "gm", "aba"); 143 User user6 = new User(5, "嘚嘚", "sd", 20, "dsz", "aba"); 144 User user7 = new User(21, "服啦服啦", "ddd", 21, "nhd", "aba"); 145 User user8 = new User(22, "啦啦", "bbb", 22, "dhsa", "aba"); 146 userList.add(user1); 147 userList.add(user2); 148 userList.add(user3); 149 userList.add(user4); 150 userList.add(user5); 151 userList.add(user6); 152 userList.add(user7); 153 userList.add(user8); 154 } 155 }
3. 關於tableModel的代碼
1 package com.test.order; 2 3 import java.util.List; 4 5 6 public class TableOrder3TableMode implements TableModel{ 7 8 private List<User> userList; 9 10 public TableOrder3TableMode(List<User> userList){ 11 this.userList = userList; 12 } 13 14 @Override 15 public Class<?> getColumnClass(int columnIndex) { 16 return String.class; 17 } 18 19 @Override 20 public int getColumnCount() { 21 22 return 6; 23 } 24 25 @Override 26 public int getRowCount() { 27 return userList.size(); 28 } 29 30 @Override 31 public String getColumnName(int columnIndex) { 32 if(columnIndex == 0) 33 return "id"; 34 if(columnIndex == 1) 35 return "用戶名"; 36 if(columnIndex == 2) 37 return "密碼"; 38 if(columnIndex == 3) 39 return "年齡"; 40 if(columnIndex == 4) 41 return "郵箱"; 42 if(columnIndex == 5) 43 return "地址"; 44 else return null; 45 46 } 47 48 @Override 49 public Object getValueAt(int rowIndex, int columnIndex) { 50 User user = userList.get(rowIndex); 51 if(columnIndex == 0) 52 return user.getId(); 53 if(columnIndex == 1) 54 return user.getUserName(); 55 if(columnIndex == 2) 56 return user.getUserPassword(); 57 if(columnIndex == 3) 58 return user.getAge(); 59 if(columnIndex == 4) 60 return user.getEmail(); 61 if(columnIndex == 5) 62 return user.getAddress(); 63 return null; 64 } 65 66 /** 67 * 如果表格可以編輯,當編輯完按下enter後則會自動調用該方法 68 */ 69 @Override 70 public void setValueAt(Object aValue, int rowIndex, int columnIndex) { 71 String s="Change at: "+rowIndex+"--- "+columnIndex+" newValue: "+aValue; 72 System.out.println(s); 73 74 } 75 76 @Override 77 public boolean isCellEditable(int rowIndex, int columnIndex) { 78 if(columnIndex == 0) 79 return false; 80 return true; 81 } 82 83 @Override 84 public void removeTableModelListener(TableModelListener l) { 85 // TODO Auto-generated method stub 86 87 } 88 @Override 89 public void addTableModelListener(TableModelListener l) { 90 // TODO Auto-generated method stub 91 92 } 93 94 public List<User> getList(){ 95 return this.userList; 96 } 97 98 public String getColumnObjectName(int columnIndex) { 99 if(columnIndex == 0) 100 return User.ID; 101 if(columnIndex == 1) 102 return User.USERNAME; 103 if(columnIndex == 2) 104 return User.USERPASSWORD; 105 if(columnIndex == 3) 106 return User.AGE; 107 if(columnIndex == 4) 108 return User.EMAIL; 109 if(columnIndex == 5) 110 return User.ADDRESS; 111 else return null; 112 } 113 114 115 }
4. 維護排序列的類(SorterColumnGroupUtil)
類的主要作用及思路:維護排序列,最多支持3列同時排序。當收到列名時,首先判斷(第一主列==null),如果為空,則將該列名設為第一主列;否則判斷(!=第一主列 && 第二主列==null),是則設為第二主列;否則判斷(!=第一主列 && !=第二主列 && 第三主列==null),是則設為第三主列;否則則說明三個主列都有了。接下來就是判斷是否等於三個主列中的其中一個,如果不等於,說明三大主列已經滿了,什麼都不用做。如果等於,然後判斷它是升序還是降序,是升序就將它變成降序,是降序將該主列清空,後面的主列向前面的主列移動。
1 package com.test.order.orderutil; 2 3 public class SorterColumnGroupUtil { 4 5 6 private static String[][] sorterArray = new String[3][2];//排序列數組 7 8 public static String getColumn1(){//獲得第一主排序列名 9 return sorterArray[0][0]; 10 } 11 public static String getColumn2(){//獲得第二主排序列名 12 return sorterArray[1][0]; 13 } 14 public static String getColumn3(){//獲得第三主排序列名 15 return sorterArray[2][0]; 16 } 17 public static String getColumn1Asc(){//獲得第一主排序的升降序 18 return sorterArray[0][1]; 19 } 20 public static String getColumn2Asc(){//獲得第二主排序的升降序 21 return sorterArray[1][1]; 22 } 23 public static String getColumn3Asc(){//獲得第三主排序的升降序 24 return sorterArray[2][1]; 25 } 26 27 //設置排序列 28 public static void setColumn(String columnName){ 29 if(columnName != null){//第一主排序不為null,則將該列設為第一主排序,默認升序 30 if(sorterArray[0][0] == null){ 31 sorterArray[0][0] = columnName; 32 sorterArray[0][1] = Constant.ASC; 33 }else if(sorterArray[1][0] == null && !sorterArray[0][0].equals(columnName)){//有第一主排序,且第二主排序不為null,則將該列設為第二主排序,默認升序 34 sorterArray[1][0] = columnName; 35 sorterArray[1][1] = Constant.ASC; 36 }else if(sorterArray[2][0] == null && !sorterArray[0][0].equals(columnName) && !sorterArray[1][0].equals(columnName)){//有第一和第二主排序,且第二主排序不為null,則將該列設為第二主排序,默認升序 37 sorterArray[2][0] = columnName; 38 sorterArray[2][1] = Constant.ASC; 39 }else{ 40 if(columnName.equals(sorterArray[0][0])){//如果等於第一主排序,則講第一主排序降序 41 if(Constant.ASC.equals(sorterArray[0][1])){ 42 sorterArray[0][1] = Constant.DESC; 43 }else if(Constant.DESC.equals(sorterArray[0][1])){//第一主排序為降序,則去掉第一主排序,二變一,三變二,三為空 44 sorterArray[0][0] = sorterArray[1][0]; 45 sorterArray[0][1] = sorterArray[1][1]; 46 47 sorterArray[1][0] = sorterArray[2][0]; 48 sorterArray[1][1] = sorterArray[2][1]; 49 50 sorterArray[2][0] = null; 51 sorterArray[2][1] = null; 52 } 53 }else if(columnName.equals(sorterArray[1][0]) ){//第二主排序 同理 54 if(Constant.ASC.equals(sorterArray[1][1])){ 55 sorterArray[1][1] = Constant.DESC; 56 }else if(Constant.DESC.equals(sorterArray[1][1])){ 57 sorterArray[1][0] = sorterArray[2][0]; 58 sorterArray[1][1] = sorterArray[2][1]; 59 60 sorterArray[2][0] = null; 61 sorterArray[2][1] = null; 62 } 63 }else if(columnName.equals(sorterArray[2][0])){//第三主排序 64 if(Constant.ASC.equals(sorterArray[2][1])){ 65 sorterArray[2][1] = Constant.DESC; 66 }else if(Constant.DESC.equals(sorterArray[2][1])){ 67 sorterArray[2][0] = null; 68 sorterArray[2][1] = null; 69 } 70 } 71 } 72 } 73 } 74 }
5. 自定義排序類,實現了comparator的類
5.1 TableOrderSorter
1 package com.test.order; 2 3 import java.util.Comparator; 4 5 public class TableOrderSorter implements Comparator<User>{ 6 7 @Override 8 public int compare(User o1, User o2) { 9 return SorterUtil.compare(o1, o2); 10 } 11 }
5.2 SorterUtil
1 package com.test.order.orderutil; 2 3 import java.lang.reflect.Method; 4 5 public class SorterUtil { 6 7 public static int compare(Object o1, Object o2) { 8 Object o1sorterColumn1 = getFieldByReflect(o1,SorterColumnGroupUtil.getColumn1()); //得到o1 對象的第一主排序列 9 Object o1sorterColumn2 = getFieldByReflect(o1,SorterColumnGroupUtil.getColumn2()); //得到o1 對象的第二主排序列 10 Object o1sorterColumn3 = getFieldByReflect(o1,SorterColumnGroupUtil.getColumn3()); //得到o1 對象的第三主排序列 11 Object o2sorterColumn1 = getFieldByReflect(o2,SorterColumnGroupUtil.getColumn1()); //得到o2 對象的第一主排序列 12 Object o2sorterColumn2 = getFieldByReflect(o2,SorterColumnGroupUtil.getColumn2()); //得到o2 對象的第二主排序列 13 Object o2sorterColumn3 = getFieldByReflect(o2,SorterColumnGroupUtil.getColumn3()); //得到o2 對象的第三主排序列 14 Object sorterColumnAsc1 = SorterColumnGroupUtil.getColumn1Asc(); //第一主排序列 15 Object sorterColumnAsc2 = SorterColumnGroupUtil.getColumn2Asc(); //第二主排序列 16 Object sorterColumnAsc3 = SorterColumnGroupUtil.getColumn3Asc(); //第三主排序列 17 if(sorterColumnAsc1 != null){//判斷 1主 18 if(Constant.ASC.equals(sorterColumnAsc1)){//1 主升 19 if(sorterColumnAsc2 != null){//判斷 2主 20 if(Constant.ASC.equals(sorterColumnAsc2)){//2 主升 21 if(sorterColumnAsc3 != null){//判斷3主 22 if(Constant.ASC.equals(sorterColumnAsc3)){//3 主升 23 return compareAsc(o1sorterColumn1,o2sorterColumn1) == 0?(compareAsc(o1sorterColumn2,o2sorterColumn2) == 0? 24 compareAsc(o1sorterColumn3,o2sorterColumn3) 25 :compareAsc(o1sorterColumn2,o2sorterColumn2)) 26 :compareAsc(o1sorterColumn1,o2sorterColumn1);//1升 2升 3升 27 }else if(Constant.DESC.equals(sorterColumnAsc3)){//3主降 28 return compareAsc(o1sorterColumn1,o2sorterColumn1) == 0?(compareAsc(o1sorterColumn2,o2sorterColumn2) == 0? 29 compareDesc(o1sorterColumn3,o2sorterColumn3) 30 :compareAsc(o1sorterColumn2,o2sorterColumn2)) 31 :compareAsc(o1sorterColumn1,o2sorterColumn1);//1升 2升 3降 32 } 33 }else{//無 3主 34 return compareAsc(o1sorterColumn1,o2sorterColumn1) == 0?(compareAsc(o1sorterColumn2,o2sorterColumn2)) 35 :compareAsc(o1sorterColumn1,o2sorterColumn1);//1升 2升 36 } 37 }else if(Constant.DESC.equals(sorterColumnAsc2)){//2主降 38 if(sorterColumnAsc3 != null){//判斷3主 39 if(Constant.ASC.equals(sorterColumnAsc3)){//3 主升 40 return compareAsc(o1sorterColumn1,o2sorterColumn1) == 0?(compareDesc(o1sorterColumn2,o2sorterColumn2) == 0? 41 compareAsc(o1sorterColumn3,o2sorterColumn3) 42 :compareDesc(o1sorterColumn2,o2sorterColumn2)) 43 :compareAsc(o1sorterColumn1,o2sorterColumn1);//1升 2降 3升 44 }else if(Constant.DESC.equals(sorterColumnAsc3)){//3主降 45 return compareAsc(o1sorterColumn1,o2sorterColumn1) == 0?(compareDesc(o1sorterColumn2,o2sorterColumn2) == 0? 46 compareDesc(o1sorterColumn3,o2sorterColumn3) 47 :compareDesc(o1sorterColumn2,o2sorterColumn2)) 48 :compareAsc(o1sorterColumn1,o2sorterColumn1);//1升 2降 3降 49 } 50 }else{//無 3主 51 return compareAsc(o1sorterColumn1,o2sorterColumn1) == 0?compareDesc(o1sorterColumn2,o2sorterColumn2) 52 :compareAsc(o1sorterColumn1,o2sorterColumn1);//1升 2降 53 } 54 } 55 }else{//無 2主 56 return compareAsc(o1sorterColumn1,o2sorterColumn1);//1 升 57 } 58 }else if(Constant.DESC.equals(sorterColumnAsc1)){//1 主降 59 if(sorterColumnAsc2 != null){//判斷 2主 60 if(Constant.ASC.equals(sorterColumnAsc2)){//2 主升 61 if(sorterColumnAsc3 != null){//判斷3主 62 if(Constant.ASC.equals(sorterColumnAsc3)){//3 主升 63 return compareDesc(o1sorterColumn1,o2sorterColumn1) == 0?(compareAsc(o1sorterColumn2,o2sorterColumn2) == 0? 64 compareAsc(o1sorterColumn3,o2sorterColumn3) 65 :compareAsc(o1sorterColumn2,o2sorterColumn2)) 66 :compareDesc(o1sorterColumn1,o2sorterColumn1);//1降 2升 3升 67 }else if(Constant.DESC.equals(sorterColumnAsc3)){//3主降 68 return compareDesc(o1sorterColumn1,o2sorterColumn1) == 0?(compareAsc(o1sorterColumn2,o2sorterColumn2) == 0? 69 compareDesc(o1sorterColumn3,o2sorterColumn3) 70 :compareAsc(o1sorterColumn2,o2sorterColumn2)) 71 :compareDesc(o1sorterColumn1,o2sorterColumn1);// 1降 2升 3降 72 } 73 }else{//無 3主 74 return compareDesc(o1sorterColumn1,o2sorterColumn1) == 0?compareAsc(o1sorterColumn2,o2sorterColumn2) 75 :compareDesc(o1sorterColumn1,o2sorterColumn1);// 1降 2升 76 } 77 }else if(Constant.DESC.equals(sorterColumnAsc2)){//2主降 78 if(sorterColumnAsc3 != null){//判斷3主 79 if(Constant.ASC.equals(sorterColumnAsc3)){//3 主升 80 return compareDesc(o1sorterColumn1,o2sorterColumn1) == 0?(compareDesc(o1sorterColumn2,o2sorterColumn2) == 0? 81 compareAsc(o1sorterColumn3,o2sorterColumn3) 82 :compareDesc(o1sorterColumn2,o2sorterColumn2)) 83 :compareDesc(o1sorterColumn1,o2sorterColumn1);//1 2 降 , 3 升 84 }else if(Constant.DESC.equals(sorterColumnAsc3)){//3主降 85 return compareDesc(o1sorterColumn1,o2sorterColumn1) == 0?(compareDesc(o1sorterColumn2,o2sorterColumn2) == 0? 86 compareDesc(o1sorterColumn3,o2sorterColumn3) 87 :compareDesc(o1sorterColumn2,o2sorterColumn2)) 88 :compareDesc(o1sorterColumn1,o2sorterColumn1);// 1 2 3 降 89 } 90 }else{//無 3主 91 return compareDesc(o1sorterColumn1,o2sorterColumn1) == 0?compareDesc(o1sorterColumn2,o2sorterColumn2) 92 :compareDesc(o1sorterColumn1,o2sorterColumn1);// 1 2 降 93 } 94 } 95 }else{//無 2主 96 return compareDesc(o1sorterColumn1,o2sorterColumn1);// 1降 97 } 98 } 99 }else{//無 1 主 100 return 0; 101 } 102 return 0; 103 } 104 105 /** 106 * 自動尋找合適的升序序選擇器 107 * @param o1 108 * @param o2 109 * @return 110 */ 111 public static int compareAsc(Object o1,Object o2){ 112 if (o1 instanceof String) { //字符串 113 return compareAsc( (String) o1, (String) o2); 114 }else if (o1 instanceof Integer) { //Integer 115 return compareAsc( (Integer) o1, (Integer) o2); 116 }else if (o1 instanceof Long) { //Long 117 return compareAsc( (Long) o1, (Long) o2); 118 }else { 119 System.err.println("未找到合適的比較器"); //找不到 120 return 0; 121 } 122 } 123 /** 124 * 自動尋找合適的降序序序選擇器 125 * @param o1 126 * @param o2 127 * @return 128 */ 129 public static int compareDesc(Object o1,Object o2){ 130 if (o1 instanceof String) { //字符串 131 return compareDesc( (String) o1, (String) o2); 132 }else if (o1 instanceof Integer) { //Integer 133 return compareDesc( (Integer) o1, (Integer) o2); 134 }else if (o1 instanceof Long) { //Integer 135 return compareDesc( (Long) o1, (Long) o2); 136 }else { 137 System.err.println("未找到合適的比較器"); //找不到 138 return 1; 139 } 140 } 141 142 /** 143 * 升序比較字符串 144 * @param s1 145 * @param s2 146 * @return 147 */ 148 public static int compareAscs(String s1, String s2){ 149 int result = 0; 150 if(StringUtils.isBlank(s1)){ 151 result = 1; 152 }else if(StringUtils.isBlank(s2)){ 153 result = -1; 154 }else{ 155 result = s1.compareTo(s2); 156 } 157 return result; 158 } 159 160 /** 161 * 降序比較字符串 162 * @param s1 163 * @param s2 164 * @return 165 */ 166 public static int compareDescs(String s1, String s2){ 167 int result = 0; 168 if(StringUtils.isBlank(s1)){ 169 result = 1; 170 }else if(StringUtils.isBlank(s2)){ 171 result = -1; 172 }else{ 173 result = s2.compareTo(s1); 174 } 175 return result; 176 } 177 178 /** 179 * 升序比較數值 180 * @param s1 181 * @param s2 182 * @return 183 */ 184 public static int compareAsc(Integer s1, Integer s2){ 185 int result = 0; 186 if(s1 <= 0){ 187 result = 1; 188 }else if(s2 <= 0){ 189 result = -1; 190 }else{ 191 result = s1.compareTo(s2); 192 } 193 return result; 194 } 195 196 /** 197 * 降序比較數值 198 * @param s1 199 * @param s2 200 * @return 201 */ 202 public static int compareDesc(Long s1, Long s2){ 203 int result = 0; 204 if(s1 <= 0){ 205 result = 1; 206 }else if(s2 <= 0){ 207 result = -1; 208 }else{ 209 result = s2.compareTo(s1); 210 } 211 return result; 212 } 213 /** 214 * 升序比較數值 215 * @param s1 216 * @param s2 217 * @return 218 */ 219 public static int compareAsc(Long s1, Long s2){ 220 int result = 0; 221 if(s1 <= 0){ 222 result = 1; 223 }else if(s2 <= 0){ 224 result = -1; 225 }else{ 226 result = s1.compareTo(s2); 227 } 228 return result; 229 } 230 231 /** 232 * 降序比較數值 233 * @param s1 234 * @param s2 235 * @return 236 */ 237 public static int compareDesc(Integer s1, Integer s2){ 238 int result = 0; 239 if(s1 <= 0){ 240 result = 1; 241 }else if(s2 <= 0){ 242 result = -1; 243 }else{ 244 result = s2.compareTo(s1); 245 } 246 return result; 247 } 248 249 /** 250 * get屬性 251 * @param field 252 * @return 253 */ 254 public static String field2MethodName(String field){ 255 StringBuffer method = null; 256 if(StringUtils.isNotBlank(field)){ 257 method = new StringBuffer(); 258 method.append("get"); 259 method.append(field.substring(0,1).toUpperCase()); 260 method.append(field.substring(1)); 261 return method.toString(); 262 }else 263 return null; 264 } 265 266 /** 267 * 通過屬性得到屬性值,使用反射的方式,只要該類有set&get方法,即可通過反射獲得該屬性 268 * @param o 269 * @param field 270 * @return 271 */ 272 public static Object getFieldByReflect(Object o,String field){ 273 Object oo = null; 274 Class<?> clazz = o.getClass(); 275 String methodName = field2MethodName(field); 276 if(clazz != null && methodName != null){ 277 try { 278 Method method = clazz.getMethod(methodName); 279 oo = method.invoke(o); 280 } catch (Exception e) { 281 e.printStackTrace(); 282 } 283 } 284 return oo; 285 } 286 287 /** 288 * 升序比較漢字字符串 289 * @param s1 290 * @param s2 291 * @return 292 */ 293 public static int compareAsc(String s1, String s2){ 294 int result = 0; 295 if(StringUtils.isBlank(s1)){ 296 result = 1; 297 }else if(StringUtils.isBlank(s2)){ 298 result = -1; 299 }else{ 300 result = Collator.getInstance(java.util.Locale.CHINA).compare(s1, s2); 301 } 302 return result; 303 } 304 /** 305 * 降序比較漢字字符串 306 * @param s1 307 * @param s2 308 * @return 309 */ 310 public static int compareDesc(String s1, String s2){ 311 int result = 0; 312 if(StringUtils.isBlank(s1)){ 313 result = 1; 314 }else if(StringUtils.isBlank(s2)){ 315 result = -1; 316 }else{ 317 result = -Collator.getInstance(java.util.Locale.CHINA).compare(s1, s2); 318 } 319 return result; 320 } 321 322 }
6. User類
1 package com.test.order; 2 3 import java.io.Serializable; 4 5 public class User implements Serializable{ 6 7 @Override 8 public String toString() { 9 StringBuffer bf = new StringBuffer(); 10 bf.append("User[:"); 11 bf.append("userName:"+userName); 12 bf.append("\nuserPassword:"+userPassword); 13 bf.append("\nage:"+age+"]"); 14 return bf.toString(); 15 } 16 17 private static final long serialVersionUID = 2764373816508835680L; 18 private Integer id; 19 private String userName; 20 private String userPassword; 21 private int age; 22 private String email; 23 private String address; 24 25 26 27 public User(Integer id, String userName, String userPassword, int age, 28 String email, String address) { 29 super(); 30 this.id = id; 31 this.userName = userName; 32 this.userPassword = userPassword; 33 this.age = age; 34 this.email = email; 35 this.address = address; 36 } 37 public User() { 38 super(); 39 // TODO Auto-generated constructor stub 40 } 41 public Integer getId() { 42 return id; 43 } 44 public void setId(Integer id) { 45 this.id = id; 46 } 47 public String getUserName() { 48 return userName; 49 } 50 public void setUserName(String userName) { 51 this.userName = userName; 52 } 53 public String getUserPassword() { 54 return userPassword; 55 } 56 public void setUserPassword(String userPassword) { 57 this.userPassword = userPassword; 58 } 59 public int getAge() { 60 return age; 61 } 62 public void setAge(int age) { 63 this.age = age; 64 } 65 public String getEmail() { 66 return email; 67 } 68 public void setEmail(String email) { 69 this.email = email; 70 } 71 public String getAddress() { 72 return address; 73 } 74 public void setAddress(String address) { 75 this.address = address; 76 } 77 78 public final static String ID = "id"; 79 public final static String USERNAME = "userName"; 80 public final static String USERPASSWORD = "userPassword"; 81 public final static String AGE = "age"; 82 public final static String EMAIL= "email"; 83 public final static String ADDRESS = "address"; 84 85 }
7. 倆作用不大的小工具
7.1 TableHeaderButton(作用:表頭圖標樣式)
1 package com.test.order; 2 3 import javax.swing.BorderFactory; 4 5 public class TableHeaderButton extends JButton{ 6 7 public TableHeaderButton(){ 8 setRolloverEnabled(true); 9 setRequestFocusEnabled(false); 10 setBorder(BorderFactory.createRaisedBevelBorder()); 11 setOpaque(true); 12 setContentAreaFilled(false); 13 setFocusable(false); 14 setHorizontalAlignment(JLabel.CENTER); 15 setHorizontalTextPosition(SwingConstants.LEFT); 16 setIconTextGap(5); 17 } 18 19 }
7.2 Constant (升降序常量:1升序,-1降序)
1 package com.test.order.orderutil; 2 3 public class Constant { 4 public static final String ASC = "1"; 5 public static final String DESC = "-1"; 6 7 }
三、項目鏈接:
1. 本人QQ,1220817583
2. CSDN鏈接:http://download.csdn.net/detail/qq_31790075/9720827