JTable中的“刷新"数据 [英] ''Refreshing'' data in JTable
问题描述
我正在制作一个GUI,它包含一个JPanel,在该JPanel中有一个JTable,我想做的是:当我单击一个按钮时,它们都会同时出现(因为我正在使用CardLayout).代码:
I'm making a GUI and it includes a JPanel, inside that JPanel there's a JTable, and what I want to do is: when I click a button, both of them appear (since I'm using CardLayout). Code:
private void teGjithaButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
parentPanel.setVisible(true);
parentPanel.removeAll();
parentPanel.add(tgjPanel);
parentPanel.repaint();
parentPanel.revalidate();
listAllCurtains();
} catch (SQLException ex) {
Logger.getLogger(MainBrillant.class.getName()).log(Level.SEVERE, null, ex);
}
}
以及listAllCurtains()的代码:
And the code for listAllCurtains():
DefaultTableModel deftm = (DefaultTableModel) allTable.getModel();
if (deftm.getRowCount() != 0) {
deftm.setRowCount(0);
}
stm = (Statement) conn.createStatement();
ResultSet rs = stm.executeQuery("select * from customerregister.curtain inner join curtainrel on curtain.code = curtainrel.curtainCode;");
while (rs.next()) {
String shifra = rs.getString("code");
String ngjyra = rs.getString("color");
String emri = rs.getString("name");
double cmimi = rs.getDouble("price");
double sasia = rs.getDouble("amount");
allCurtains.add(new Curtain(shifra, ngjyra, emri, cmimi, sasia));
}
Object[] row = new Object[5];
for (int i = 0; i < allCurtains.size(); i++) {
row[0] = allCurtains.get(i).getShifra();
row[1] = allCurtains.get(i).getEmri();
row[2] = allCurtains.get(i).getNgjyra();
row[3] = allCurtains.get(i).getCmimi();
row[4] = allCurtains.get(i).getSasia();
deftm.addRow(row);
}
}
问题是,即使我重新单击该按钮,即使有一部分代码可以确保数据不重复,也是如此:
The problem is that when i re-click the button, eventhough there's this part of the code to ensure the data is not duplicated:
if (deftm.getRowCount() != 0) {
deftm.setRowCount(0);
}
每次单击按钮时,它仍继续将相同的数据插入表中.我不知道为什么会这样,非常感谢您的帮助.
It still continues to insert the same data into table each time the button is clicked. I can't figure out why this is happening, and I'd really appreciate your help.
推荐答案
也许您应该在决定再次发布数据之前清除JTable模型的行.代替您的:
Perhaps you should Clear your JTable model of its rows before you decide to reissue the data again. Instead of your:
if (deftm.getRowCount() != 0) {
deftm.setRowCount(0);
}
代码,也许使用简单的 clearTable()方法,例如:
code, perhaps use a simple clearTable() method like:
private void clearJTable(DefaultTableModel yourTableModel) {
while (yourTableModel.getRowCount() > 0) {
for (int i = 0; i < yourTableModel.getRowCount(); i++) {
yourTableModel.removeRow(i);
}
}
}
,然后在您的代码中:
DefaultTableModel deftm = (DefaultTableModel) allTable.getModel();
clearJTable(deftm);
stm = (Statement) conn.createStatement();
ResultSet rs = stm.executeQuery("select * from customerregister.curtain inner join curtainrel on curtain.code = curtainrel.curtainCode;");
while (rs.next()) {
String shifra = rs.getString("code");
String ngjyra = rs.getString("color");
String emri = rs.getString("name");
double cmimi = rs.getDouble("price");
....................................
....................................
....................................
这篇关于JTable中的“刷新"数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!