/**取对象序列化后的MD5值*/ public static String md5(Object data) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); new ObjectOutputStream(baos).writeObject(data); byte[] array = MessageDigest.getInstance("MD5").digest(baos.toByteArray()); StringBuffer sb = new StringBuffer(32); for (int i = 0; i < array.length; i++) {//array.length = 16 /*3位16进制数取后两位*/ sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3)); } return sb.toString(); } public static void main(String[] args) throws Exception { /*准备3行3列的数据*/ Vector<Vector<Object>> rowData = new Vector<Vector<Object>>(); Vector<Object> row1 = new Vector<Object>(); row1.add(1);row1.add("张三");row1.add(new Date()); rowData.add(row1); Vector<Object> row2 = new Vector<Object>(); row2.add(2);row2.add("李四");row2.add(new Date()); rowData.add(row2); Vector<Object> row3 = new Vector<Object>(); row3.add(3);row3.add("王五");row3.add(new Date()); rowData.add(row3); /*表头名称*/ Vector<String> columnNames = new Vector<String>(); columnNames.add("序号");columnNames.add("姓名");columnNames.add("日期"); //JTable(Vector rowData, Vector columnNames) JTable table = new JTable(rowData, columnNames); JFrame frame = new JFrame("Test"); frame.getContentPane().add(new JScrollPane(table)); frame.setSize(800, 600);frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); String data0 = md5(rowData); /*1、什么也没做数据没有变化,不会提示保存*/ if(!data0.equals(md5(rowData)))JOptionPane.showMessageDialog(frame, "提示1:是否保存修改?"); /*2、修改某个单元格的值,内容发送了变化,会提示保存*/ rowData.get(0).set(1, "张三1");table.repaint(); if(!data0.equals(md5(rowData)))JOptionPane.showMessageDialog(frame, "提示2:是否保存修改?"); /*3、单元格改回原来的值,内容没有变化,不会提示保存*/ rowData.get(0).set(1, "张三");table.repaint(); if(!data0.equals(md5(rowData)))JOptionPane.showMessageDialog(frame, "提示3:是否保存修改?"); }
时间: 2024-11-07 07:17:59