在jTable(自动保存)中编辑后如何保存单元格中的数据?
Posted
技术标签:
【中文标题】在jTable(自动保存)中编辑后如何保存单元格中的数据?【英文标题】:How can i save the data from the cell after editing in jTable(autosave)? 【发布时间】:2013-03-03 18:48:34 【问题描述】:我有一个 JTable,其中填充了来自矢量的数据,我希望在编辑数据后将其自动保存在数据库中。
Vector columnNames = new Vector();
Vector data = new Vector();
Connection conn = dbConnect.connect();
String sql = "Select date, frequency, type, payeereceivee, amount, comments from schedule where date ='" + outputdate + "'";
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
ResultSetMetaData metaData = resultSet.getMetaData();
int columns = metaData.getColumnCount();
for (int i = 1; i <= columns; i++)
columnNames.addElement(metaData.getColumnName(i));
while (resultSet.next())
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++)
row.addElement(resultSet.getObject(i));
data.addElement(row);
resultSet.close();
statement.close();
tbl = new JTable(data, columnNames);
TableColumn column;
for (int i = 0; i < tbl.getColumnCount(); i++)
column = tbl.getColumnModel().getColumn(i);
column.setMaxWidth(250);
【问题讨论】:
从DefaultTableModel
扩展并将其用作表模型。在您的自定义模型中,覆盖 setValue()
以更新绑定到模型的数据并更新数据库。
能否说的更清楚一些,谢谢
【参考方案1】:
上述 cmets 的意思是创建自己的 TableModel
(例如通过扩展 DefaultTableModel
)并覆盖/实现 setValueAt
以更新 DB。
请参阅下面的 sn-p。确保您不在 EDT(事件调度线程,如果您不知道它是什么,请查看“Swing 中的并发”)上执行 DB 更新,例如使用 SwingWorker
。
import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class TestTable2
protected void initUI()
Vector<String> columnNames = new Vector<String>();
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
Connection conn = dbConnect.connect();
String sql = "Select date, frequency, type, payeereceivee, amount, comments from schedule where date ='" + outputdate + "'";
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
ResultSetMetaData metaData = resultSet.getMetaData();
int columns = metaData.getColumnCount();
for (int i = 1; i <= columns; i++)
columnNames.addElement(metaData.getColumnName(i));
while (resultSet.next())
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++)
row.addElement(resultSet.getObject(i));
data.addElement(row);
resultSet.close();
statement.close();
JTable tbl = new JTable(new DefaultTableModel(data, columnNames)
@Override
public void setValueAt(Object aValue, int row, int column)
// Here update DB with a SwingWorker and the new provided value
super.setValueAt(aValue, row, column);
);
TableColumn column;
for (int i = 0; i < tbl.getColumnCount(); i++)
column = tbl.getColumnModel().getColumn(i);
column.setMaxWidth(250);
JFrame frame = new JFrame(TestTable2.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollpane = new JScrollPane(tbl);
frame.add(scrollpane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable()
@Override
public void run()
new TestTable2().initUI();
);
【讨论】:
以上是关于在jTable(自动保存)中编辑后如何保存单元格中的数据?的主要内容,如果未能解决你的问题,请参考以下文章