问题描述
不常用swing,感觉很陌生.希望大家帮帮忙.用JTable显示数据库里一个表的信息,有按钮"增加",按下后在表格的最后增加一行,可以往上填数据,然后点击"保存",保存到数据库,也可以选择一行进行编辑,点"保存"保存到数据库.现在我点"增加"按钮后可以增加一行,但是往上填数据的时候,填完一格转到另一格后数据就不见了,不知道怎么回事.代码贴出来,可以运行看看,就三个类,如下:(所有的分都给了)//模型package product;import java.util.ArrayList;import javax.swing.table.AbstractTableModel;import javax.swing.table.DefaultTableModel;public class ProductTableModel extends AbstractTableModel {private ArrayList list = new ArrayList();private String[] column = { "Product Name", "Product Description", "Status" };public ProductTableModel() {}public ProductTableModel(ArrayList list) {this();setList(list);}public int getColumnCount() {return column.length;}public int getRowCount() {return list.size();}public Object getValueAt(int arg0, int arg1) {ProductBean p = (ProductBean) list.get(arg0);return getPropertyValueByCol(p, arg1);}public void addList(int index, ProductBean p) {if (index < 0 || index > list.size() - 1) {list.add(p);fireTableRowsInserted(list.size(), list.size());} else {list.add(index, p);fireTableRowsInserted(index, index);}}public boolean deleteList(int index) {if (index >= 0 && index < list.size()) {list.remove(index);fireTableRowsDeleted(index, index);return true;} elsereturn false;}public boolean updateList(int index, ProductBean p) {if (index >= 0 && index < list.size()) {list.set(index, p);fireTableRowsUpdated(index, index);return true;} elsereturn false;}public ProductBean getList(int index) {if (index >= 0 && index < list.size()) {return (ProductBean) list.get(index);} elsereturn null;}public ArrayList getList() {return list;}public void setList(ArrayList list) {this.list = list;fireTableDataChanged();}public String getColumnName(int i) {return column[i];}public void setColumn(String[] column) {this.column = column;}public Object getPropertyValueByCol(ProductBean p, int col) {switch (col) {case 0:return p.getProduct_name();case 1:return p.getProduct_desc();case 2:return p.getProduct_status();}return null;}public boolean isCellEditable(int row, int column) { return true; }}//主要的显示package product;import java.awt.BorderLayout;import java.awt.Container;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.ArrayList;import java.util.LinkedList;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.event.TableModelEvent;import javax.swing.event.TableModelListener;public class ProductFrame extends JFrame {private JPanel pnlTop = null;private JLabel lblProductName = null;private JLabel lblProductDesc = null;private JLabel lblProductStatus = null; private JButton btnAdd = null;private JButton btnDelete = null;private JButton btnUpdate = null;private JScrollPane panTable = null;private JTable tblProduct = null;private ProductTableModel tmdProduct = null;private ArrayList lstProduct = null;public ProductTableModel getTmdProduct() {if (null == tmdProduct) {tmdProduct = new ProductTableModel(lstProduct);return tmdProduct;}return tmdProduct;}public JTable getTblProduct() {if (null == tblProduct) {tblProduct = new JTable(getTmdProduct());tblProduct.setEnabled(true);tblProduct.setRowSelectionAllowed(true);return tblProduct;}return tblProduct;}public JScrollPane getPanTable() {if (null == panTable) {panTable = new JScrollPane();panTable.setViewportView(getTblProduct());return panTable;}return panTable;}public JPanel getPnlTop() {if (null == pnlTop) {pnlTop = new JPanel();pnlTop.setLayout(new FlowLayout(FlowLayout.CENTER));pnlTop.add(getLblProductName()); pnlTop.add(getLblProductDesc());pnlTop.add(getLblProductStatus());pnlTop.add(getBtnAdd());pnlTop.add(getBtnDelete());pnlTop.add(getBtnUpdate());return pnlTop;}return pnlTop;}public JLabel getLblProductName() {if (null == lblProductName) {lblProductName = new JLabel("Product Name");return lblProductName;}return lblProductName;}public JLabel getLblProductDesc() {if (null == lblProductDesc) {lblProductDesc = new JLabel("Product Description");return lblProductDesc;}return lblProductDesc;}public JLabel getLblProductStatus() {if (null == lblProductStatus) {lblProductStatus = new JLabel("Status");return lblProductStatus;}return lblProductStatus;}public JButton getBtnAdd() {if (null == btnAdd) {btnAdd = new JButton("添加");btnAdd.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {addProductBean();}});return btnAdd;}return btnAdd;}public JButton getBtnDelete() {if (null == btnDelete) {btnDelete = new JButton("删除");btnDelete.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {deleteProduct();}});return btnDelete;}return btnDelete;}public JButton getBtnUpdate() {if (null == btnUpdate) {btnUpdate = new JButton("更新");btnUpdate.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {updateProduct();}});return btnUpdate;}return btnUpdate;}public void addProductBean() {ProductBean p = new ProductBean();getTmdProduct().addList(getTblProduct().getSelectedRow(), p);}public void updateProduct() {ProductBean p = getTmdProduct().getList(getTblProduct().getSelectedRow());if (p != null) {getTmdProduct().updateList(getTblProduct().getSelectedRow(), p);}}public void deleteProduct() {getTmdProduct().deleteList(getTblProduct().getSelectedRow());}public void initData() {lstProduct = new ArrayList();}public ProductFrame() {initData();Container c = this.getContentPane();c.setLayout(new BorderLayout());c.add(getPnlTop(), BorderLayout.NORTH);c.add(getPanTable(), BorderLayout.CENTER);this.setSize(new Dimension(800, 600));this.setVisible(true);}public static void main(String[] args) {new ProductFrame();}}//产品类package product;public class ProductBean {private String product_name;private String product_desc;private String product_status;public String getProduct_name() {return product_name;}public void setProduct_name(String product_name) {this.product_name = product_name;}public String getProduct_desc() {return product_desc;}public void setProduct_desc(String product_desc) {this.product_desc = product_desc;}public String getProduct_status() {return product_status;}public void setProduct_status(String product_status) {this.product_status = product_status;}}
解决方案
让ProductTableModel覆盖setValueAt()方法:public void setValueAt(Object arg0, int arg1, int arg2) {ProductBean p = (ProductBean) list.get(arg1);switch (arg2) {case 0:p.setProduct_name(arg0.toString());return;case 1:p.setProduct_desc(arg0.toString());return;case 2:p.setProduct_status(arg0.toString());return;}super.setValueAt(arg0, arg1, arg2);}