【发布时间】:2023-12-20 06:29:01
【问题描述】:
在我的 JTable 中,更新完成后,需要刷新以显示更改:
public class RecordTableGUI extends JFrame implements ActionListener {
private String newName;
private JTable table;
private RecordTableModel myModel;
private JButton editButton;
public RecordTableGUI() {
myModel = new RecordTableModel();
table = new JTable(myModel);
add(new JScrollPane(table), BorderLayout.CENTER);
add(buttonsPanel(), BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(700, 550);
setLocation(300, 80);
setVisible(true);
}
public JPanel buttonsPanel() {
JPanel bPanel = new JPanel();
editButton = new JButton("Edit");
editButton.addActionListener(this);
bPanel.add(editButton);
return bPanel;
}
@Override
public void actionPerformed(ActionEvent e) {
if (table.getSelectedRow() > -1) {
Object oldName = table.getValueAt(table.getSelectedRow(), 1);
UpdateGUIDialog updDialog = new UpdateGUIDialog(this,String.valueOf(oldName), this);
int rowToEdit = table.getSelectedRow();
int rowToModel = table.convertRowIndexToView(rowToEdit);
Object nameID = table.getValueAt(table.getSelectedRow(), 0);
myModel.updateRow(rowToModel, nameID, getNewName());
} else {
JOptionPane.showMessageDialog(null, "Select a row");
}
}
}
模型类:
public class RecordTableModel extends AbstractTableModel {
Connection con;
Statement statement;
ResultSet result;
String dbUrl = "jdbc:mysql://localhost/mydb";
String query = "Select * from mytable";
ArrayList<String> cols = new ArrayList<String>();
ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
public RecordTableModel() {
try {
con = DriverManager.getConnection(dbUrl, "root", "2323");
statement = con.createStatement();
result = statement.executeQuery(query);
int c = result.getMetaData().getColumnCount();
for (int i = 1; i <= c; i++) {
cols.add(result.getMetaData().getColumnName(i));
}
while (result.next()) {
ArrayList<String> eachRow = new ArrayList<String>();
for (int i = 1; i <= c; i++) {
eachRow.add(result.getString(i));
}
data.add(eachRow);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
}
if (statement != null) {
statement.close();
}
} catch (SQLException sqlee) {
sqlee.printStackTrace();
}
}
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public int getColumnCount() {
return cols.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
ArrayList<String> selectedRow = data.get(rowIndex);
return selectedRow.get(columnIndex);
}
@Override
public String getColumnName(int col) {
return cols.get(col);
}
public void updateRow(int modelRow, Object nameID, Object newName) {
String query = "update mytable set name = '" + newName + "' where id = " + nameID;
Connection conn;
PreparedStatement pstate;
try {
conn = DriverManager.getConnection(dbUrl, "root", "2323");
pstate = conn.prepareStatement(query);
pstate.executeUpdate();
fireTableRowsUpdated(tableRow, tableRow);
fireTableDataChanged();
fireTableCellUpdated(modelRow, 1);
} catch (SQLException sql) {
sql.printStackTrace();
}
}
【问题讨论】:
-
您之前已经被问过好几次了——给我们发sscce,然后您继续无视我们的请求。我们怎么知道为什么你的代码不能基于这些废话 sn-ps 工作?如果您不做出贡献,为什么还要麻烦帮助您?
-
@HovercraftFullOfEels 该解决方案在这里不起作用!
-
that solution does not work in here!-- 什么解决方案?使用sscce 向我们展示你做错了什么不会“工作”?讨厌这么说,但你发布的内容也不会奏效。如果我们无法理解您的问题,您将无法获得知识渊博的帮助。这取决于你——你有多少需要解决这个问题?如果它很棒,那就付出必要的努力。如果没有,那就没什么大不了了。 -
发布 SSCCE 几乎总能奏效。在您的情况下,您需要在不使用数据库的情况下发布一个演示问题的 SSCCE,因为我们无权访问您的数据库。
-
不足之处在于代码未显示。期间。