Search code examples
javamysqlswingjtableresultset

Result Set not always being shown in the JTable


I am creating an application in Java which retrieves some information from a MySQL database and then displays it in a JTable on the screen.

The code I am using to get and put the information into a DefaultListModel<> is shown below. The DefaultListModel<> is then fed to the JTable which displays the information onto the screen.

SwingWorker<Void, Void> worker = new SwingWorker< Void, Void>() {
        
        @Override
        public Void doInBackground() {
            
            String SQL = "SELECT * FROM AddABill";
            
            try {
                Statement stmt = GlobalVars.conn.createStatement();
                
                ResultSet rs = stmt.executeQuery(SQL);
                
                menuOptions.clear();
                
                while(rs.next()){
                    
                    menuOptions.addElement(rs.getString(1) + " - " + rs.getString(2));
                }
                
                rs.close();
                stmt.close();
            }
            
            catch (SQLException e) {
                System.out.println("An Error Was Detected! :/");
            }
            
            return null;
        }
        
        @Override
        public void done(){
            loadingGif.setVisible(false);
        }
        
    };
    
    worker.execute();

The issue I am having is that sometimes the information is displayed onto the screen and sometimes it isn't. Since there is no change in the query used I am assuming it is something to do with the MySQL connection.

The JTable and MenuOptions are setup like this

menuOptions = new DefaultListModel<>();
menuOptions.addElement("");
    
menuList = new JList<>(menuOptions);
menuList.setFont(new Font("Myriad Hebrew", Font.PLAIN, 32));
menuList.setBackground(Color.decode("#005952"));
menuList.setForeground(Color.decode("#BEC423"));
menuList.setSelectionBackground(Color.decode("#660033"));
menuList.setSelectionForeground(Color.decode("#FFFFFF"));

So the question I am wondering is how can I make sure that the information is always displayed on the screen. A picture of when it is displayed and not displayed is shown below.

Displayed:

Displayed

Not Displayed:

Not Displayed

On a final note to get the information to show I have to keep on re-running the code until it is eventually shown.


Solution

  • I have solved the issue by adding the information into the table from the done() function. The code is below

    SwingWorker<Void, Void> worker = new SwingWorker< Void, Void>() {
            
            String[] billItems = null;
            
            @Override
            public Void doInBackground() {
                
                String SQL = "SELECT * FROM AddABill";
                
                try {
                    Statement stmt = GlobalVars.conn.createStatement();
                    
                    ResultSet rs = stmt.executeQuery(SQL);
                    
                    //Set The Cursor To Last Position
                    rs.last();
                    
                    int totalRows = rs.getRow();
                    
                    billItems = new String[totalRows];
                    
                    //Set The Cursor Back To The Start
                    rs.beforeFirst();
                    
                    while(rs.next()){
                        int rowNum = rs.getRow() - 1;
                        billItems[rowNum] = rs.getString(1) + " - " + rs.getString(2);
                    }
                    
                    rs.close();
                    stmt.close();
                }
                
                catch (SQLException e) {
                    System.out.println("An Error Was Detected! :/");
                }
                
                return null;
            }
            
            @Override
            public void done(){
                menuOptions.clear();
                
                for(int i = 0; i < billItems.length; i++){
                    menuOptions.addElement(billItems[i]);
                }
                
                loadingGif.setVisible(false);
            }
            
        };