什麼是上述評論的意思是創建自己的TableModel
(例如通過將DefaultTableModel
),並覆蓋/實施setValueAt
來更新數據庫。
請參閱下面的代碼片段。確保在EDT上不執行數據庫更新(事件分派線程,如果您不知道它是什麼,請檢查「Swing中的併發」),例如使用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();
}
});
}
}
從['DefaultTableModel'](http://docs.oracle.com/javase/7/docs/api/javax/swing/table/DefaultTableModel.html)擴展並使用它作爲表模型。在您的自定義模型中,覆蓋['setValue()'](http://docs.oracle.com/javase/7/docs/api/javax/swing/table/DefaultTableModel.html#setValueAt(java.lang.Object, %20int,%20int))來更新綁定到模型的數據並更新數據庫。 – 2013-03-03 19:06:31
你能更清楚嗎,謝謝 – 2013-03-03 19:19:25