Note that there are some explanatory texts on larger screens.

plurals
  1. POupdating JList with DefaultListModel
    text
    copied!<p>I am using an embedded database to query the name of an entry and put it in a <code>JList</code>. When the program runs, the list is populated fine.</p> <p>I have made a function that is supposed to initialize and refresh the list called <code>populateList()</code>.</p> <p>Here are the relevant parts of my code:</p> <pre><code>public class GUI extends JFrame{ private int maxBankNr; private BankAccountDAO bankAccountDAO; private DBManager dbm; ... public GUI(){ initComponents(); //sets up the Swing GUI this.dbm = new DBManager(); this.bankAccountDAO = dbm.getBankAccountDAO(); ... populateList(); } private void populateList(){ updateAll = false; //this seems to stop baAccountListValueChanged from throwing an exception this.maxBankNr = bankAccountDAO.getMaxBankNr(); //max number of bank accounts in database BankAccount ba; DefaultListModel dlm = new DefaultListModel(); baAccountList.setModel(dlm); for(int i = 1; i &lt;= this.maxBankNr; i++){ ba = bankAccountDAO.getBankAccount(i); dlm.addElement(ba.getName()); } updateAll = true; } ... private void baRefreshButtonActionPerformed(java.awt.event.ActionEvent evt){ populateList(); } ... </code></pre> <p>My problem is that <code>populateList()</code> works fine when the program starts, but when it is called from <code>baRefreshButtonActionPerformed</code>, it appears to do nothing. The list stays exactly the same.</p> <p>I have tried lots of different approaches, like using a Vector, using <code>JList.setListData()</code>, revalidating, validating and repainting all relevant containers. I have also tried using different types of <code>ListModel</code>.</p> <p>Also, I have read that calling <code>fireContentsChanged()</code> should work, but <code>DefaultListModel</code> doesn't allow it, and I'm sure it gets called automatically anyway.</p> <p>I have spent hours looking for a fix, most sites I've visited say the same things that I have tried, although none of them are working.</p> <p>I hope this is enough information, let me know if you need to know anything else, thanks.</p> <p><strong>EDIT</strong> I have finally managed to fix this, much to my relief :D</p> <p>It had nothing to do with my <strong>ListModel</strong> at all, but my <strong>BankAccountDAO</strong> class. This is the old version:</p> <pre><code>package com.accounts; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class BankAccountDAO { private String insertBankAccountSQL = "INSERT INTO accounts.bankaccounts(bankNr, sortCode, accountNumber, balance, interest, details, name) " + "VALUES (?, ?, ?, ?, ?, ?, ?)"; private String getBankAccountSQL = "SELECT bankNr, sortCode, accountNumber, balance, interest, details, name " + "FROM accounts.bankaccounts WHERE bankNr = ?"; private Connection conn = null; private PreparedStatement pstmt = null; private int maxBankNr = 0; public BankAccountDAO(Connection theConn){ this.conn = theConn; try{ this.pstmt = conn.prepareStatement(getBankAccountSQL); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT MAX(bankNr) FROM accounts.bankaccounts"); if(rs.next()){ maxBankNr = rs.getInt(1); } rs.close(); stmt.close(); }catch(SQLException se){ printSQLException(se); } } public int getMaxBankNr(){ return(this.maxBankNr); } public void insertBankAccount(int bankNr, int sortCode, int accountNumber, double balance, double interest, String details, String name){ PreparedStatement ins = null; try{ ins = conn.prepareStatement(insertBankAccountSQL); ins.setInt(1, bankNr); ins.setInt(2, sortCode); ins.setInt(3, accountNumber); ins.setDouble(4, balance); ins.setDouble(5, interest); ins.setString(6, details); ins.setString(7, name); ins.execute(); }catch(SQLException se){ printSQLException(se); } } public BankAccount getBankAccount(int targetBankNr){ BankAccount ba = null; try{ pstmt.clearParameters(); pstmt = conn.prepareStatement(getBankAccountSQL); pstmt.setInt(1, targetBankNr); ResultSet rs = pstmt.executeQuery(); if(rs.next()){ int bankNr = rs.getInt("bankNr"); int sortCode = rs.getInt("sortCode"); int accountNumber = rs.getInt("accountNumber"); double balance = rs.getDouble("balance"); double interest = rs.getDouble("interest"); String details = rs.getString("details"); String name = rs.getString("name"); ba = new BankAccount(bankNr, sortCode, accountNumber, balance, interest, details, name); } rs.close(); }catch(SQLException se){ printSQLException(se); } return ba; } private void printSQLException(SQLException se){ while(se != null){ System.out.print("SQLException: State: " + se.getSQLState()); System.out.println("Severity: " + se.getErrorCode()); System.out.println(se.getMessage()); se = se.getNextException(); } } } </code></pre> <p>I have changed the <strong>getMaxBankNr()</strong> getter as follows:</p> <pre><code>public int getMaxBankNr(){ try{ Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT MAX(bankNr) FROM accounts.bankaccounts"); if(rs.next()){ maxBankNr = rs.getInt(1); } }catch(SQLException e){ printSQLException(e); } return(this.maxBankNr); } </code></pre> <p>When <strong>this.maxBankNr = bankAccountDAO.getMaxBankNr()</strong> was called, it wasn't returning an updated <strong>maxBankNr.</strong> This required a new SQL statement to return the proper value.</p> <p>Anyone using a database with a list in the same way as me (i noticed quite a few people with the same problem as me while googling) should make sure that the for loop <strong>really</strong> knows how many times to loop.</p> <p>I would have posted this as an answer, but I can't answer my own question for 8 hours, because I don't have enough points.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload