I am using an embedded database to query the name of an entry and put it in a JList
. When the program runs, the list is populated fine.
I have made a function that is supposed to initialize and refresh the list called populateList()
.
Here are the relevant parts of my 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 <= this.maxBankNr; i++){
ba = bankAccountDAO.getBankAccount(i);
dlm.addElement(ba.getName());
}
updateAll = true;
}
...
private void baRefreshButtonActionPerformed(java.awt.event.ActionEvent evt){
populateList();
}
...
My problem is that populateList()
works fine when the program starts, but when it is called from baRefreshButtonActionPerformed
, it appears to do nothing. The list stays exactly the same.
I have tried lots of different approaches, like using a Vector, using JList.setListData()
, revalidating, validating and repainting all relevant containers. I have also tried using different types of ListModel
.
Also, I have read that calling fireContentsChanged()
should work, but DefaultListModel
doesn't allow it, and I'm sure it gets called automatically anyway.
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.
I hope this is enough information, let me know if you need to know anything else, thanks.
EDIT I have finally managed to fix this, much to my relief :D
It had nothing to do with my ListModel at all, but my BankAccountDAO class. This is the old version:
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();
}
}
}
I have changed the getMaxBankNr() getter as follows:
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);
}
When this.maxBankNr = bankAccountDAO.getMaxBankNr() was called, it wasn't returning an updated maxBankNr. This required a new SQL statement to return the proper value.
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 really knows how many times to loop.
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.
Hmm... This is a tough one. I think that that if populateList()
works by itself, that baRefresButtonActionPerformed
is not being executed itself.
I would try a regular ActionEvent
and ActionPerformed
as a listener or put some kind of output in it, like if baRefreshButtonActionPerformed
, have it show a popup box saying the override has been executed.
Happy coding!
Normally you should call one of the following methods to notify the JList object of the changed data as apropriate:
fireContentsChanged(Object source, int index0, int index1)
fireIntervalAdded(Object source, int index0, int index1)
fireIntervalRemoved(Object source, int index0, int index1)
Please check API doc for AbstractListModel .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.