Home  >  Article  >  Java  >  How to Improve GUI Performance and Auto-Update Table After Row Deletion in Java?

How to Improve GUI Performance and Auto-Update Table After Row Deletion in Java?

Susan Sarandon
Susan SarandonOriginal
2024-11-19 12:37:02644browse

How to Improve GUI Performance and Auto-Update Table After Row Deletion in Java?

AbstractTableModel GUI Display Issue

The issue with the GUI in this example seems to be related to accessing the database asynchronously, which can cause inconsistencies in the table display. To resolve this, the ResultSet should be retrieved in the background to avoid blocking the event dispatch thread. The data can be published in chunks and added to the table model incrementally to provide a more fluid display.

Here's a modified version of the code using a SwingWorker to retrieve and process the results in the background:

public class Gui2 extends JFrame {
    // ...

    public Gui2(Connection conn) {
        // ...

        SwingWorker<List<Row>, Integer> worker = new SwingWorker<List<Row>, Integer>() {
            @Override
            protected List<Row> doInBackground() throws Exception {
                try {
                    while (rs.next()) {
                        Row row = new Row();
                        row.ID = rs.getInt(1);
                        row.name = rs.getString(2);
                        publish(row);
                    }
                } catch (SQLException e) {
                    e.printStackTrace(System.err);
                }
                return null;
            }

            @Override
            protected void process(List<Row> chunks) {
                int n = getRowCount();
                for (Row row : chunks) {
                    tableData.add(row);
                }
                fireTableRowsInserted(n, n + chunks.size());
            }
        };
        worker.execute();
        
        // ...
    }
}

The worker will retrieve the rows in the background and publish them in chunks. The process() method will add the rows to the TableModel and update the table display incrementally.

Automatic Table Update After Row Deletion

To automatically update the table after a row is deleted, the delete operation should be performed in the TableModel instead of the GUI. The TableModel should have a delete() method that removes the row from the underlying data and fires a table rows deleted event to notify the table component of the change. Here's a modified version of the delete() method:

public class TableModel extends AbstractTableModel {
    // ...

    public void delete(int rowIndex) {
        // ...
        try {
            PreparedStatement pre = conn.prepareStatement(query);

            pre.executeUpdate();

            // Remove the row from the data
            tableData.remove(rowIndex);

            // Fire table rows deleted event
            fireTableRowsDeleted(rowIndex, rowIndex);

            JOptionPane.showMessageDialog(null, "Row Deleted Successfully");
        } catch (Exception e1) {
            JOptionPane.showMessageDialog(null, e1.getMessage());
        }
    }
}

With these modifications, the table will automatically update after a row is deleted, providing a more user-friendly and responsive interface.

The above is the detailed content of How to Improve GUI Performance and Auto-Update Table After Row Deletion in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn