JTable动态增加一行

问题描述

不常用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);}

时间: 2024-10-31 07:42:10

JTable动态增加一行的相关文章

html-实现点击 图标动态增加一行或多行,删除一行或多行

问题描述 实现点击 图标动态增加一行或多行,删除一行或多行 <td colspan =""4""><input class=""w100"" id=""meetingdate1"" name=""meetingdate"" type=""text"" validateGroup="

利用jquery给指定的table动态添加一行、删除一行的方法_jquery

$("#mytable tr").find("td:nth-child(1)") 1表示获取每行的第一列 $("#mytable tr").find("td:nth-child(3)") 3表示获取每行的第三列 今天在项目中,刚好用到给指定的table添加一行.删除一行,就直接找google,搜出来的东西不尽如人意,不是功能不好就是千篇一律,简直浪费时间还不讨好,于是乎就自己动手封装个,现就把代码分享出来,避免大伙重复造轮子,

jQuery实现立体式数字动态增加(animate方法)_jquery

1.HTML结构 <div class="integral">已有<span class="ii"></span>积分</div> 2.js <script type="text/javascript" src="js/jquery.js" ></script> <script type="text/javascript" src

JQuery实现动态表格点击按钮表格增加一行_jquery

功能实现:点击添加按钮,表格增加一行并给其name属性赋予的值,方便获取 点击删除,自动删除这一行 JQuery中定义一个count变量 var count = 1; function add() { var tbl = document.all.ci; var rows = tbl.rows.length; var tr = tbl.insertRow(rows); var e_id = tr.insertCell(0); e_id.innerHTML = '<input type="t

js动态增加删除行

js动态增加删除行代码,很实用. 所做过的多个项目中均用到js动态添加删除行的应用,在写过多种方案后,最终整理出这个最为方便应用的方案,以备日后拿来便用: 以下是DEMO全部代码: <html>  <head>   <title>Ace Test</title> <script language="JavaScript">      var cGetRow=-99999;   function insertrow(){   v

app-Android中怎样实现动态添加一行EditView

问题描述 Android中怎样实现动态添加一行EditView 类似于联系人app中添加联系人号码的功能,可以按+后添加一栏EditView,增加一个号码,或是按按钮后增加修改联系人姓名(增加前缀后缀)的EditView. 解决方案 就拿添加联系人来说的:首先有一个按钮,你点击(这里是点击事件)后,会弹出一个界面,这个界面你用来添加手机,添加成功后,点击按钮后就添加了(这里点击按钮 后是对数据库执行添加操作的,如果是修改的话,你根据点击的位置得到联系人的Id,然后到数据库里修改就行了). 这里主

jquery 动态增加删除行的简单实例(推荐)_jquery

最近写程序,碰巧有动态增加删除行,下面就记录一下 html就不写了,也没有什么,直接上核心了 新增行 function addRow(obj){ //获得table一共有多少行,方便追加的时候给序号赋值 var length = $("#grid tr").length; //获得当前是第几行,以便追加的时候,在该行下进行新增,我这里跨的级别比较多,根据实际层级去定义 var current = $(obj).parent().parent().parent().prevAll().l

jquery动态增加删减表格行特效_jquery

基于jQuery表格增加删除代码是一款动态增加删减表格行特效代码.分享给大家供大家参考.具体如下: 运行效果截图如下: 具体代码如下: html代码: <div style="width:720px;margin:20px auto;"> <table id="tab11" style="display: none"> <tbody> <tr> <td height="30"

在表单中动态增加文本框

动态|文本框 <SCRIPT language="javascript">var row = 0; function insertElement() {  var nod=document.createElement("span");  document.form1.insertBefore(nod);  nod.innerHTML="<br>row"+row+"<input type=text name=