概述
这两天公司让做一个Jtable表格的排序,首先按A列排序,在A列相等时按B列排序,B列相等时按C列排序,ABC三列可以任意指定,最多分三列,这样的一个需求。由于我是大神,所以必须做了出来。ok,不自恋了,先上效果图,然后上代码,最后上项目。这也将是我以后的一贯风格,懂得分享才能走的更高!
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 }
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
转载于:https://www.cnblogs.com/shuaishuaiguo/p/6220044.html
最后
以上就是缓慢墨镜为你收集整理的Jtable 表格按多列排序(支持中文汉字排序)的全部内容,希望文章能够帮你解决Jtable 表格按多列排序(支持中文汉字排序)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复