Jtable 表格按多列排序(支持中文汉字排序)

  这两天公司让做一个Jtable表格的排序,首先按A列排序,在A列相等时按B列排序,B列相等时按C列排序,ABC三列可以任意指定,最多分三列,这样的一个需求。由于我是大神,所以必须做了出来。ok,不自恋了,先上效果图,然后上代码,最后上项目。这也将是我以后的一贯风格,懂得分享才能走的更高!

Jtable 表格按多列排序(支持中文汉字排序)Jtable 表格按多列排序(支持中文汉字排序)
 
  简单描述一下,瞅准某列,第一次点击升序,第二次点击降序,第三次点击取消排序。图为先按密码升序,然后按用户名升序,最后按年龄降序。
 
  一、 准备工作,及整体思路
 
  我的环境,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)
 package com.test.order;

 import java.awt.Dimension;

 public class TableOrder3Test {
// private static int width = 600;
// private static int height = 600; public static void main(String[] args) {
displayTable(); } public static void displayTable(){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// 设置程序界面外观
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception localException1) {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
} catch (Exception localException2) {
localException2.printStackTrace();
}
} JFrame tableFrame = new JFrame("表格排序"); TableOrder3Table order3Talbe = new TableOrder3Table(); tableFrame.setContentPane(new JScrollPane(order3Talbe.initUI())); Toolkit kit = Toolkit.getDefaultToolkit();//获得窗口超类
Dimension dim = kit.getScreenSize();//获得当前屏幕
// tableFrame.setLocation(((int)dim.getWidth()-width)/2,
// (int)(dim.getHeight()-height)/2);//设置frame距离左和上的偏移量,即窗口显示的位置
// tableFrame.setLocationRelativeTo(null);
tableFrame.setSize(dim);//调整屏幕大小
// tableFrame.setSize(200, 200);//调整屏幕大小
tableFrame.setResizable(true);//是否可调整大小
tableFrame.setVisible(true);//是否显示
tableFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//按叉之后执行哪个操作
}
});
}
}
  2. 关于Jtable类的代码(下面的3,4,5,6,7中的类都将在此类中出现,注意它们在此类中的角色)
 package com.test.order;

 import java.awt.Component;

 public class TableOrder3Table{

     private TableOrder3TableMode tabelMode;
private JTable order3Table;
private List<User> userList;
private TableOrderSorter tableOrderSorterUtil; public JTable initUI(){ getData();
tabelMode = new TableOrder3TableMode(userList);//获取model
order3Table = new JTable();
order3Table.setModel(tabelMode);//设置model
addHeaderEvent();//添加对表头排序事件
setSorterIconToTableHeader(order3Table.getTableHeader());//添加表头排序图标
return order3Table;
} /**
* 添加对表头排序事件
*/
public void addHeaderEvent(){
order3Table.getTableHeader().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if(evt.getButton() == MouseEvent.BUTTON1 && evt.getClickCount() == 1){ //判断左键右键,点击了几次
int selectedColumn = order3Table.getTableHeader().columnAtPoint(evt.getPoint());//得到该鼠标点击列的column,从0开始
String sortColumn = tabelMode.getColumnObjectName(selectedColumn);//获得排序列名
SorterColumnGroupUtil.setColumn(sortColumn);//设置排序列,SorterColumnGroupUtil类便是用来维护排序列的工具类
userSort();//表格列排序
}
}
});
} /**
* 为表格表头添加排序图标
* @param tableHeader
*/
@SuppressWarnings("serial")
public void setSorterIconToTableHeader(JTableHeader tableHeader) {
tableHeader.setDefaultRenderer(new DefaultTableCellRenderer(){//添加tableheader渲染器 private Font TABLE_FONT = new Font("宋体", Font.PLAIN, 15);
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JButton btn = new TableHeaderButton();//JideTool.newJideButton();
btn.setText(value == null ? "" : value.toString());
btn.setFont(TABLE_FONT);
String sortColumnName ="";
sortColumnName = tabelMode.getColumnObjectName(column);
if(sortColumnName.equals(SorterColumnGroupUtil.getColumn1())){
if (Constant.ASC.equals(SorterColumnGroupUtil.getColumn1Asc())) {
btn.setIcon(getIconResource("arr-up1.png"));
} else if (Constant.DESC.equals(SorterColumnGroupUtil.getColumn1Asc())){
btn.setIcon(getIconResource("arr-down1.png"));
}
}else if(sortColumnName.equals(SorterColumnGroupUtil.getColumn2())){
if (Constant.ASC.equals(SorterColumnGroupUtil.getColumn2Asc())) {
btn.setIcon(getIconResource("arr-up2.png"));
} else if (Constant.DESC.equals(SorterColumnGroupUtil.getColumn2Asc())){
btn.setIcon(getIconResource("arr-down2.png"));
}
}else if(sortColumnName.equals(SorterColumnGroupUtil.getColumn3())){
if (Constant.ASC.equals(SorterColumnGroupUtil.getColumn3Asc())) {
btn.setIcon(getIconResource("arr-up3.png"));
} else if (Constant.DESC.equals(SorterColumnGroupUtil.getColumn3Asc())){
btn.setIcon(getIconResource("arr-down3.png"));
}
}
return btn;
}
});
} /**
* 稍稍封装了获得icon的过程,默认搜索
*/
public static ImageIcon getIconResource(String iconName)
{
return new ImageIcon("icon/"+iconName);
} /**
* 表格列排序
*/
public void userSort() {
Collections.sort(tabelMode.getList(), getOrderTableSorter());//将要排序的数据及自定义的排序类传给Collections
order3Table.updateUI();
} /**
* 获得排序类接口
* @return
*/
public TableOrderSorter getOrderTableSorter(){
if(tableOrderSorterUtil == null){
tableOrderSorterUtil = new TableOrderSorter();
}
return tableOrderSorterUtil;
} /**
* 设置表格样式
*/
public void setStyle(){
order3Table.getColumnModel().getColumn(2).setPreferredWidth(10);
order3Table.setRowHeight(25);
} /**
* 添加对表格内容的事件
*/
public void addEvent(){
order3Table.addMouseListener(new java.awt.event.MouseAdapter(){
public void mouseClicked(MouseEvent e) {//仅当鼠标单击时响应
//得到选中的行列的索引值
int r= order3Table.getSelectedRow();
int c= order3Table.getSelectedColumn();
//得到选中的单元格的值,表格中都是字符串
Object value= order3Table.getValueAt(r, c);
String info=r+"行"+c+"列值 : "+value.toString();
javax.swing.JOptionPane.showMessageDialog(null,info);
}
});
} /**
* 获取模拟数据
*/
public void getData(){
userList = new ArrayList<User>();
User user1 = new User(1, "大元帅", "哈", 18, "ddd", "eee");
User user2 = new User(11, "帅帅", "bbb", 18, "drdfrt", "aba");
User user3 = new User(10, "阿哥好帅", "dsd", 18, "dnca", "aba");
User user4 = new User(2, "巴拉拉", "sdf", 18, "wer", "aba");
User user5 = new User(3, "小魔仙", "abc", 18, "gm", "aba");
User user6 = new User(5, "嘚嘚", "sd", 20, "dsz", "aba");
User user7 = new User(21, "服啦服啦", "ddd", 21, "nhd", "aba");
User user8 = new User(22, "啦啦", "bbb", 22, "dhsa", "aba");
userList.add(user1);
userList.add(user2);
userList.add(user3);
userList.add(user4);
userList.add(user5);
userList.add(user6);
userList.add(user7);
userList.add(user8);
}
}

  3. 关于tableModel的代码

 package com.test.order;

 import java.util.List;

 public class TableOrder3TableMode implements TableModel{

     private List<User> userList;

     public TableOrder3TableMode(List<User> userList){
this.userList = userList;
} @Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
} @Override
public int getColumnCount() { return 6;
} @Override
public int getRowCount() {
return userList.size();
} @Override
public String getColumnName(int columnIndex) {
if(columnIndex == 0)
return "id";
if(columnIndex == 1)
return "用户名";
if(columnIndex == 2)
return "密码";
if(columnIndex == 3)
return "年龄";
if(columnIndex == 4)
return "邮箱";
if(columnIndex == 5)
return "地址";
else return null; } @Override
public Object getValueAt(int rowIndex, int columnIndex) {
User user = userList.get(rowIndex);
if(columnIndex == 0)
return user.getId();
if(columnIndex == 1)
return user.getUserName();
if(columnIndex == 2)
return user.getUserPassword();
if(columnIndex == 3)
return user.getAge();
if(columnIndex == 4)
return user.getEmail();
if(columnIndex == 5)
return user.getAddress();
return null;
} /**
* 如果表格可以编辑,当编辑完按下enter后则会自动调用该方法
*/
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
String s="Change at: "+rowIndex+"--- "+columnIndex+" newValue: "+aValue;
System.out.println(s); } @Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
if(columnIndex == 0)
return false;
return true;
} @Override
public void removeTableModelListener(TableModelListener l) {
// TODO Auto-generated method stub }
@Override
public void addTableModelListener(TableModelListener l) {
// TODO Auto-generated method stub } public List<User> getList(){
return this.userList;
} public String getColumnObjectName(int columnIndex) {
if(columnIndex == 0)
return User.ID;
if(columnIndex == 1)
return User.USERNAME;
if(columnIndex == 2)
return User.USERPASSWORD;
if(columnIndex == 3)
return User.AGE;
if(columnIndex == 4)
return User.EMAIL;
if(columnIndex == 5)
return User.ADDRESS;
else return null;
} }

  4. 维护排序列的类(SorterColumnGroupUtil)

  类的主要作用及思路:维护排序列,最多支持3列同时排序。当收到列名时,首先判断(第一主列==null),如果为空,则将该列名设为第一主列;否则判断(!=第一主列 && 第二主列==null),是则设为第二主列;否则判断(!=第一主列 && !=第二主列 && 第三主列==null),是则设为第三主列;否则则说明三个主列都有了。接下来就是判断是否等于三个主列中的其中一个,如果不等于,说明三大主列已经满了,什么都不用做。如果等于,然后判断它是升序还是降序,是升序就将它变成降序,是降序将该主列清空,后面的主列向前面的主列移动。

 package com.test.order.orderutil;

 public class SorterColumnGroupUtil {

 private static String[][] sorterArray = new String[3][2];//排序列数组 

     public static String getColumn1(){//获得第一主排序列名
return sorterArray[0][0];
}
public static String getColumn2(){//获得第二主排序列名
return sorterArray[1][0];
}
public static String getColumn3(){//获得第三主排序列名
return sorterArray[2][0];
}
public static String getColumn1Asc(){//获得第一主排序的升降序
return sorterArray[0][1];
}
public static String getColumn2Asc(){//获得第二主排序的升降序
return sorterArray[1][1];
}
public static String getColumn3Asc(){//获得第三主排序的升降序
return sorterArray[2][1];
} //设置排序列
public static void setColumn(String columnName){
if(columnName != null){//第一主排序不为null,则将该列设为第一主排序,默认升序
if(sorterArray[0][0] == null){
sorterArray[0][0] = columnName;
sorterArray[0][1] = Constant.ASC;
}else if(sorterArray[1][0] == null && !sorterArray[0][0].equals(columnName)){//有第一主排序,且第二主排序不为null,则将该列设为第二主排序,默认升序
sorterArray[1][0] = columnName;
sorterArray[1][1] = Constant.ASC;
}else if(sorterArray[2][0] == null && !sorterArray[0][0].equals(columnName) && !sorterArray[1][0].equals(columnName)){//有第一和第二主排序,且第二主排序不为null,则将该列设为第二主排序,默认升序
sorterArray[2][0] = columnName;
sorterArray[2][1] = Constant.ASC;
}else{
if(columnName.equals(sorterArray[0][0])){//如果等于第一主排序,则讲第一主排序降序
if(Constant.ASC.equals(sorterArray[0][1])){
sorterArray[0][1] = Constant.DESC;
}else if(Constant.DESC.equals(sorterArray[0][1])){//第一主排序为降序,则去掉第一主排序,二变一,三变二,三为空
sorterArray[0][0] = sorterArray[1][0];
sorterArray[0][1] = sorterArray[1][1]; sorterArray[1][0] = sorterArray[2][0];
sorterArray[1][1] = sorterArray[2][1]; sorterArray[2][0] = null;
sorterArray[2][1] = null;
}
}else if(columnName.equals(sorterArray[1][0]) ){//第二主排序 同理
if(Constant.ASC.equals(sorterArray[1][1])){
sorterArray[1][1] = Constant.DESC;
}else if(Constant.DESC.equals(sorterArray[1][1])){
sorterArray[1][0] = sorterArray[2][0];
sorterArray[1][1] = sorterArray[2][1]; sorterArray[2][0] = null;
sorterArray[2][1] = null;
}
}else if(columnName.equals(sorterArray[2][0])){//第三主排序
if(Constant.ASC.equals(sorterArray[2][1])){
sorterArray[2][1] = Constant.DESC;
}else if(Constant.DESC.equals(sorterArray[2][1])){
sorterArray[2][0] = null;
sorterArray[2][1] = null;
}
}
}
}
}
}

  5. 自定义排序类,实现了comparator的类

    5.1 TableOrderSorter

 package com.test.order;

 import java.util.Comparator;

 public class TableOrderSorter implements Comparator<User>{

     @Override
public int compare(User o1, User o2) {
return SorterUtil.compare(o1, o2);
}
}

    5.2 SorterUtil

 package com.test.order.orderutil;

 import java.lang.reflect.Method;

 public class SorterUtil {

     public static int compare(Object o1, Object o2) {
Object o1sorterColumn1 = getFieldByReflect(o1,SorterColumnGroupUtil.getColumn1());  //得到o1 对象的第一主排序列
Object o1sorterColumn2 = getFieldByReflect(o1,SorterColumnGroupUtil.getColumn2());  //得到o1 对象的第二主排序列
Object o1sorterColumn3 = getFieldByReflect(o1,SorterColumnGroupUtil.getColumn3()); //得到o1 对象的第三主排序列
Object o2sorterColumn1 = getFieldByReflect(o2,SorterColumnGroupUtil.getColumn1()); //得到o2 对象的第一主排序列
Object o2sorterColumn2 = getFieldByReflect(o2,SorterColumnGroupUtil.getColumn2()); //得到o2 对象的第二主排序列
Object o2sorterColumn3 = getFieldByReflect(o2,SorterColumnGroupUtil.getColumn3()); //得到o2 对象的第三主排序列
Object sorterColumnAsc1 = SorterColumnGroupUtil.getColumn1Asc();  //第一主排序列
Object sorterColumnAsc2 = SorterColumnGroupUtil.getColumn2Asc();  //第二主排序列
Object sorterColumnAsc3 = SorterColumnGroupUtil.getColumn3Asc();  //第三主排序列
if(sorterColumnAsc1 != null){//判断 1主
if(Constant.ASC.equals(sorterColumnAsc1)){//1 主升
if(sorterColumnAsc2 != null){//判断 2主
if(Constant.ASC.equals(sorterColumnAsc2)){//2 主升
if(sorterColumnAsc3 != null){//判断3主
if(Constant.ASC.equals(sorterColumnAsc3)){//3 主升
return compareAsc(o1sorterColumn1,o2sorterColumn1) == 0?(compareAsc(o1sorterColumn2,o2sorterColumn2) == 0?
compareAsc(o1sorterColumn3,o2sorterColumn3)
:compareAsc(o1sorterColumn2,o2sorterColumn2))
:compareAsc(o1sorterColumn1,o2sorterColumn1);//1升 2升 3升
}else if(Constant.DESC.equals(sorterColumnAsc3)){//3主降
return compareAsc(o1sorterColumn1,o2sorterColumn1) == 0?(compareAsc(o1sorterColumn2,o2sorterColumn2) == 0?
compareDesc(o1sorterColumn3,o2sorterColumn3)
:compareAsc(o1sorterColumn2,o2sorterColumn2))
:compareAsc(o1sorterColumn1,o2sorterColumn1);//1升 2升 3降
}
}else{//无 3主
return compareAsc(o1sorterColumn1,o2sorterColumn1) == 0?(compareAsc(o1sorterColumn2,o2sorterColumn2))
:compareAsc(o1sorterColumn1,o2sorterColumn1);//1升 2升
}
}else if(Constant.DESC.equals(sorterColumnAsc2)){//2主降
if(sorterColumnAsc3 != null){//判断3主
if(Constant.ASC.equals(sorterColumnAsc3)){//3 主升
return compareAsc(o1sorterColumn1,o2sorterColumn1) == 0?(compareDesc(o1sorterColumn2,o2sorterColumn2) == 0?
compareAsc(o1sorterColumn3,o2sorterColumn3)
:compareDesc(o1sorterColumn2,o2sorterColumn2))
:compareAsc(o1sorterColumn1,o2sorterColumn1);//1升 2降 3升
}else if(Constant.DESC.equals(sorterColumnAsc3)){//3主降
return compareAsc(o1sorterColumn1,o2sorterColumn1) == 0?(compareDesc(o1sorterColumn2,o2sorterColumn2) == 0?
compareDesc(o1sorterColumn3,o2sorterColumn3)
:compareDesc(o1sorterColumn2,o2sorterColumn2))
:compareAsc(o1sorterColumn1,o2sorterColumn1);//1升 2降 3降
}
}else{//无 3主
return compareAsc(o1sorterColumn1,o2sorterColumn1) == 0?compareDesc(o1sorterColumn2,o2sorterColumn2)
:compareAsc(o1sorterColumn1,o2sorterColumn1);//1升 2降
}
}
}else{//无 2主
return compareAsc(o1sorterColumn1,o2sorterColumn1);//1 升
}
}else if(Constant.DESC.equals(sorterColumnAsc1)){//1 主降
if(sorterColumnAsc2 != null){//判断 2主
if(Constant.ASC.equals(sorterColumnAsc2)){//2 主升
if(sorterColumnAsc3 != null){//判断3主
if(Constant.ASC.equals(sorterColumnAsc3)){//3 主升
return compareDesc(o1sorterColumn1,o2sorterColumn1) == 0?(compareAsc(o1sorterColumn2,o2sorterColumn2) == 0?
compareAsc(o1sorterColumn3,o2sorterColumn3)
:compareAsc(o1sorterColumn2,o2sorterColumn2))
:compareDesc(o1sorterColumn1,o2sorterColumn1);//1降 2升 3升
}else if(Constant.DESC.equals(sorterColumnAsc3)){//3主降
return compareDesc(o1sorterColumn1,o2sorterColumn1) == 0?(compareAsc(o1sorterColumn2,o2sorterColumn2) == 0?
compareDesc(o1sorterColumn3,o2sorterColumn3)
:compareAsc(o1sorterColumn2,o2sorterColumn2))
:compareDesc(o1sorterColumn1,o2sorterColumn1);// 1降 2升 3降
}
}else{//无 3主
return compareDesc(o1sorterColumn1,o2sorterColumn1) == 0?compareAsc(o1sorterColumn2,o2sorterColumn2)
:compareDesc(o1sorterColumn1,o2sorterColumn1);// 1降 2升
}
}else if(Constant.DESC.equals(sorterColumnAsc2)){//2主降
if(sorterColumnAsc3 != null){//判断3主
if(Constant.ASC.equals(sorterColumnAsc3)){//3 主升
return compareDesc(o1sorterColumn1,o2sorterColumn1) == 0?(compareDesc(o1sorterColumn2,o2sorterColumn2) == 0?
compareAsc(o1sorterColumn3,o2sorterColumn3)
:compareDesc(o1sorterColumn2,o2sorterColumn2))
:compareDesc(o1sorterColumn1,o2sorterColumn1);//1 2 降 , 3 升
}else if(Constant.DESC.equals(sorterColumnAsc3)){//3主降
return compareDesc(o1sorterColumn1,o2sorterColumn1) == 0?(compareDesc(o1sorterColumn2,o2sorterColumn2) == 0?
compareDesc(o1sorterColumn3,o2sorterColumn3)
:compareDesc(o1sorterColumn2,o2sorterColumn2))
:compareDesc(o1sorterColumn1,o2sorterColumn1);// 1 2 3 降
}
}else{//无 3主
return compareDesc(o1sorterColumn1,o2sorterColumn1) == 0?compareDesc(o1sorterColumn2,o2sorterColumn2)
:compareDesc(o1sorterColumn1,o2sorterColumn1);// 1 2 降
}
}
}else{//无 2主
return compareDesc(o1sorterColumn1,o2sorterColumn1);// 1降
}
}
}else{//无 1 主
return 0;
}
return 0;
} /**
* 自动寻找合适的升序序选择器
* @param o1
* @param o2
* @return
*/
public static int compareAsc(Object o1,Object o2){
if (o1 instanceof String) { //字符串
return compareAsc( (String) o1, (String) o2);
}else if (o1 instanceof Integer) { //Integer
return compareAsc( (Integer) o1, (Integer) o2);
}else if (o1 instanceof Long) { //Long
return compareAsc( (Long) o1, (Long) o2);
}else {
System.err.println("未找到合适的比较器"); //找不到
return 0;
}
}
/**
* 自动寻找合适的降序序序选择器
* @param o1
* @param o2
* @return
*/
public static int compareDesc(Object o1,Object o2){
if (o1 instanceof String) { //字符串
return compareDesc( (String) o1, (String) o2);
}else if (o1 instanceof Integer) { //Integer
return compareDesc( (Integer) o1, (Integer) o2);
}else if (o1 instanceof Long) { //Integer
return compareDesc( (Long) o1, (Long) o2);
}else {
System.err.println("未找到合适的比较器"); //找不到
return 1;
}
} /**
* 升序比较字符串
* @param s1
* @param s2
* @return
*/
public static int compareAscs(String s1, String s2){
int result = 0;
if(StringUtils.isBlank(s1)){
result = 1;
}else if(StringUtils.isBlank(s2)){
result = -1;
}else{
result = s1.compareTo(s2);
}
return result;
} /**
* 降序比较字符串
* @param s1
* @param s2
* @return
*/
public static int compareDescs(String s1, String s2){
int result = 0;
if(StringUtils.isBlank(s1)){
result = 1;
}else if(StringUtils.isBlank(s2)){
result = -1;
}else{
result = s2.compareTo(s1);
}
return result;
} /**
* 升序比较数值
* @param s1
* @param s2
* @return
*/
public static int compareAsc(Integer s1, Integer s2){
int result = 0;
if(s1 <= 0){
result = 1;
}else if(s2 <= 0){
result = -1;
}else{
result = s1.compareTo(s2);
}
return result;
} /**
* 降序比较数值
* @param s1
* @param s2
* @return
*/
public static int compareDesc(Long s1, Long s2){
int result = 0;
if(s1 <= 0){
result = 1;
}else if(s2 <= 0){
result = -1;
}else{
result = s2.compareTo(s1);
}
return result;
}
/**
* 升序比较数值
* @param s1
* @param s2
* @return
*/
public static int compareAsc(Long s1, Long s2){
int result = 0;
if(s1 <= 0){
result = 1;
}else if(s2 <= 0){
result = -1;
}else{
result = s1.compareTo(s2);
}
return result;
} /**
* 降序比较数值
* @param s1
* @param s2
* @return
*/
public static int compareDesc(Integer s1, Integer s2){
int result = 0;
if(s1 <= 0){
result = 1;
}else if(s2 <= 0){
result = -1;
}else{
result = s2.compareTo(s1);
}
return result;
} /**
* get属性
* @param field
* @return
*/
public static String field2MethodName(String field){
StringBuffer method = null;
if(StringUtils.isNotBlank(field)){
method = new StringBuffer();
method.append("get");
method.append(field.substring(0,1).toUpperCase());
method.append(field.substring(1));
return method.toString();
}else
return null;
} /**
* 通过属性得到属性值,使用反射的方式,只要该类有set&get方法,即可通过反射获得该属性
* @param o
* @param field
* @return
*/
public static Object getFieldByReflect(Object o,String field){
Object oo = null;
Class<?> clazz = o.getClass();
String methodName = field2MethodName(field);
if(clazz != null && methodName != null){
try {
Method method = clazz.getMethod(methodName);
oo = method.invoke(o);
} catch (Exception e) {
e.printStackTrace();
}
}
return oo;
} /**
* 升序比较汉字字符串
* @param s1
* @param s2
* @return
*/
public static int compareAsc(String s1, String s2){
int result = 0;
if(StringUtils.isBlank(s1)){
result = 1;
}else if(StringUtils.isBlank(s2)){
result = -1;
}else{
result = Collator.getInstance(java.util.Locale.CHINA).compare(s1, s2);
}
return result;
}
/**
* 降序比较汉字字符串
* @param s1
* @param s2
* @return
*/
public static int compareDesc(String s1, String s2){
int result = 0;
if(StringUtils.isBlank(s1)){
result = 1;
}else if(StringUtils.isBlank(s2)){
result = -1;
}else{
result = -Collator.getInstance(java.util.Locale.CHINA).compare(s1, s2);
}
return result;
} }

  6. User类

 package com.test.order;

 import java.io.Serializable;

 public class User implements Serializable{

     @Override
public String toString() {
StringBuffer bf = new StringBuffer();
bf.append("User[:");
bf.append("userName:"+userName);
bf.append("\nuserPassword:"+userPassword);
bf.append("\nage:"+age+"]");
return bf.toString();
} private static final long serialVersionUID = 2764373816508835680L;
private Integer id;
private String userName;
private String userPassword;
private int age;
private String email;
private String address; public User(Integer id, String userName, String userPassword, int age,
String email, String address) {
super();
this.id = id;
this.userName = userName;
this.userPassword = userPassword;
this.age = age;
this.email = email;
this.address = address;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
} public final static String ID = "id";
public final static String USERNAME = "userName";
public final static String USERPASSWORD = "userPassword";
public final static String AGE = "age";
public final static String EMAIL= "email";
public final static String ADDRESS = "address"; }

  7. 倆作用不大的小工具

    7.1 TableHeaderButton(作用:表头图标样式)

 package com.test.order;

 import javax.swing.BorderFactory;

 public class TableHeaderButton extends JButton{

     public TableHeaderButton(){
setRolloverEnabled(true);
setRequestFocusEnabled(false);
setBorder(BorderFactory.createRaisedBevelBorder());
setOpaque(true);
setContentAreaFilled(false);
setFocusable(false);
setHorizontalAlignment(JLabel.CENTER);
setHorizontalTextPosition(SwingConstants.LEFT);
setIconTextGap(5);
} }

    7.2  Constant (升降序常量:1升序,-1降序)

 package com.test.order.orderutil;

 public class Constant {
public static final String ASC = "1";
public static final String DESC = "-1"; }

三、项目链接:

1. 本人QQ,1220817583

2. CSDN链接:http://download.csdn.net/detail/qq_31790075/9720827

上一篇:TPshop之邮箱注册配置教程--附加常见问题集合


下一篇:Java Thread.interrupt interrupted