当我点击按钮时,为什么这个JTable不会更新/刷新?

时间:2015-02-14 14:37:10

标签: java swing jdbc concurrency jtable

我试图允许用户删除记录/编辑记录,然后一旦他们完成了他们可以点击刷新并且JTable应该更新但它不想要!有什么想法吗?

我尝试过重绘,重新验证,验证,无论是否在桌面,面板或框架本身上调用它们都不会起作用。

提前致谢:)

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

    }

}

0 个答案:

没有答案