0

I am currently creating a desktop application using JAVA and h2 database using CardLayout and Netbeans GUI builder. This app basically do CRUD operation and used to save customer data, purchases and list of products.

I load the data from database into a JTable so when data grows, the loading time begin to slow down and the application will seems to be froze or unresponsive to the user. After few days of googling, I have came across to SwingWorker, Thread, Runnable and EDT(not sure what else more).

I run the codes below in the constructor. I think the problem is here.

private void updateTable() {
        Vector<Object> columnNames = new Vector<Object>();
        Vector<Object> data = new Vector<Object>();
        Connection conn = new DBConnection().connect();
        try {
            Statement st = conn.createStatement();
            String numOfRow = cboNumberOfRow.getSelectedItem().toString();
            if (numOfRow == "ALL") {
                numOfRow = "-1";
            }
            String query = "select c.custid as \"id\",c.custname as \"Customer Name\",c.custic as \"IC\",c.custphoneno as \"Tel No.\",e.pointearned as \"Point Earned\", e.totalspent as \"Total Spent\" from customer c join expenses e on c.custid = e.custid group by c.custid limit " + numOfRow;

            ResultSet rs = st.executeQuery(query);
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();

            //  Get column names
            for (int i = 1; i <= columns; i++) {
                columnNames.addElement(md.getColumnLabel(i)); //md.getColumnName will return original table column name instead from the query itself
            }

            //  Get row data
            while (rs.next()) {
                Vector<Object> row = new Vector<Object>(columns);

                for (int i = 1; i <= columns; i++) {
                    row.addElement(rs.getObject(i));
                }

                data.addElement(row);
            }

            rs.close();
            st.close();
            conn.close();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex);
        }

        DefaultTableModel model = new DefaultTableModel(data, columnNames) {
            @Override
            public Class getColumnClass(int column) {
                switch (column) {
                    case 1:
                        return String.class;
                    case 4:
                        return Double.class;
                    case 5:
                        return Double.class;
                    default:
                        return String.class;
                }
            }

            @Override
            public boolean isCellEditable(int row, int col) {
                return false;
            }
        };

        customerTable.setModel(model); //set custom model

    }

Hope someone can point out my mistakes and clear picture of doing it correctly. Thanks in advance.

Chan Chun Weng
  • 876
  • 2
  • 16
  • 32
  • 1
    For [example](http://stackoverflow.com/questions/17414109/populate-jtable-with-large-number-of-rows/17415635#17415635) and [example](http://stackoverflow.com/questions/15124904/populating-jtable-using-database-data/15125161#15125161) – MadProgrammer Jun 11 '16 at 09:45
  • @MadProgrammer Thank you!! I followed this http://stackoverflow.com/questions/17414109/populate-jtable-with-large-number-of-rows/17415635#17415635 and it works just fine, the GUI loads first then the data comes after. I used `new TableSwingWorker().execute();` to load the data. One question, can I use this same code to refresh my jtable once a new record is added? – Chan Chun Weng Jun 11 '16 at 10:16
  • That depends, if at the end of the process your building a new TableModel, then yes. My personal approach would be trying to generate a delta between what you have and what's new in the database and only update those rows which have changed – MadProgrammer Jun 11 '16 at 22:23
  • @MadProgrammer yes, the method rebuild the TableModel and I would give a try for your approach. Thank you. – Chan Chun Weng Jun 12 '16 at 04:58
  • That depends a lot on what you have, for example, you could create a query based on the values you have in the table, excluding those rows you already have for example – MadProgrammer Jun 12 '16 at 05:43
  • @MadProgrammer Erm, not quite understand what you have said, can you explain more? – Chan Chun Weng Jun 12 '16 at 06:37
  • Use your select statement to filter out the rows you already have – MadProgrammer Jun 12 '16 at 07:02

0 Answers0