''Refreshing'' data in JTable

帅比萌擦擦* 提交于 2019-12-20 07:48:30

问题


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);
    }
}  

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.


回答1:


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);
}

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);
        }
    }
}

and then in your code:

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");
        ....................................
        ....................................
        ....................................


来源:https://stackoverflow.com/questions/41783797/refreshing-data-in-jtable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!