0

I'm trying to allow the user to delete a record/edit a record and then once they've done that they can click refresh and the JTable should update but it doesn't want to! Any ideas?

I have tried repaint, revalidate, validate and none of these work regardless of if i call them on the table, panel or the frame itself.

Thanks in advance :)

public class Test extends JPanel {

    public Test(CardLayout card, JPanel panelCont) {
        init();
    }

    public void init() {

        System.out.println("this");
        Connection c = null;
        Statement stmt = null;

        try {
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:test.db"); //In this case it connects to the test.db 
            stmt = c.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT COL1, COL2, COL3, COL4, COL5, COL6"
                    + ", COL7, COL8, COL9 FROM DBTEST;");

            JFrame frame = new JFrame("TESTDB");
            JPanel panel = new JPanel();
            JPanel subpan = new JPanel();

            JButton delete = new JButton("Delete");
            JButton refresh = new JButton("Refresh");
            subpan.add(refresh);

            subpan.add(delete);


            panel.setLayout(new BorderLayout());

            JTable table = new JTable(buildTableModel(rs));
            table.getTableHeader().setReorderingAllowed(false);

            panel.add(new JScrollPane(table), BorderLayout.CENTER);
            panel.add(subpan, BorderLayout.SOUTH);
            frame.add(panel);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
            frame.setSize(1000, 500);

            stmt.close();
            rs.close();
            c.close();



              refresh.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    System.out.println("refreshed");
                }
            });



            delete.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    int selected = table.getSelectedRow();
                    if (selected == -1) {
                        JOptionPane.showMessageDialog(null, "No row selected. Please select a row.");
                    } else {
                        int reply = JOptionPane.showConfirmDialog(null, "Are you sure you wish to delete "
                                + "the selected record? This cannot be undone.", "Delete Confirmation",
                                JOptionPane.YES_NO_OPTION);
                        if (reply == JOptionPane.YES_OPTION) {

                            Connection c = null;
                            Statement stmt = null;
                            try {
                                Class.forName("org.sqlite.JDBC");
                                c = DriverManager.getConnection("jdbc:sqlite:test.db");
                                c.setAutoCommit(false);
                                System.out.println("Opened database successfully");

                                stmt = c.createStatement();

                                ResultSet rs = stmt.executeQuery("SELECT * FROM VEHICLETEST;");
                                int y = 0;
                                int id = 60;
                                while (rs.next()) {
                                    if (y == selected) {
                                        id = rs.getInt("id");
                                    }
                                    y++;
                                }
                                String sql = "DELETE from DBTEST where COL9=" + id + ";";
                                stmt.executeUpdate(sql);
                                c.commit();
                                rs.close();
                                stmt.close();
                                c.close();
                                frame.dispose();


                            } catch (Exception e) {
                                System.err.println(e.getClass().getName() + ": " + e.getMessage());
                                System.exit(0);
                            }
                        }
                    }
                }
            });

        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            System.exit(0);
        }
    }

    public static DefaultTableModel buildTableModel(ResultSet rs)
            throws SQLException {

        ResultSetMetaData metaData = rs.getMetaData();

        // names of columns
        Vector<String> columnNames = new Vector<String>();
        int columnCount = metaData.getColumnCount();
        for (int column = 1; column <= columnCount; column++) {
            columnNames.add(metaData.getColumnName(column));
        }

        // data of the table
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while (rs.next()) {
            Vector<Object> vector = new Vector<Object>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(rs.getObject(columnIndex));
            }
            data.add(vector);
        }

        return new DefaultTableModel(data, columnNames);

    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
TheRapture87
  • 1,403
  • 3
  • 21
  • 31
  • 1. to split JDBC with model (inside model), 2. update (initialized) model indside JDBC (move code from model and variable vector add to data), 3. add valriables column and data to model, 4. add model to JTable, 5. in this case (from code posted here) Swing GUi is unresponsible to all mouse & key events, until JDBC ended, mode in Oracle tutorial Concurency in Swing – mKorbel Feb 14 '15 at 14:48
  • I'm still confused :( – TheRapture87 Feb 14 '15 at 17:17
  • 1
    Try `JDBCAdapter`, cited [here](http://stackoverflow.com/a/21069575/230513) and [here](http://stackoverflow.com/a/22183184/230513). – trashgod Feb 14 '15 at 18:25

0 Answers0