java - updating JList with DefaultListModel -
i using embedded database query name of entry , put in jlist
. when program runs, list populated fine.
i have made function supposed initialize , refresh list called populatelist()
.
here relevant parts of code:
public class gui extends jframe{ private int maxbanknr; private bankaccountdao bankaccountdao; private dbmanager dbm; ... public gui(){ initcomponents(); //sets swing gui this.dbm = new dbmanager(); this.bankaccountdao = dbm.getbankaccountdao(); ... populatelist(); } private void populatelist(){ updateall = false; //this seems stop baaccountlistvaluechanged throwing exception this.maxbanknr = bankaccountdao.getmaxbanknr(); //max number of bank accounts in database bankaccount ba; defaultlistmodel dlm = new defaultlistmodel(); baaccountlist.setmodel(dlm); for(int = 1; <= this.maxbanknr; i++){ ba = bankaccountdao.getbankaccount(i); dlm.addelement(ba.getname()); } updateall = true; } ... private void barefreshbuttonactionperformed(java.awt.event.actionevent evt){ populatelist(); } ...
my problem populatelist()
works fine when program starts, when called barefreshbuttonactionperformed
, appears nothing. list stays same.
i have tried lots of different approaches, using vector, using jlist.setlistdata()
, revalidating, validating , repainting relevant containers. have tried using different types of listmodel
.
also, have read calling firecontentschanged()
should work, defaultlistmodel
doesn't allow it, , i'm sure gets called automatically anyway.
i have spent hours looking fix, sites i've visited same things have tried, although none of them working.
i hope enough information, let me know if need know else, thanks.
edit have managed fix this, relief :d
it had nothing listmodel @ all, bankaccountdao class. 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 accounts.bankaccounts(banknr, sortcode, accountnumber, balance, interest, details, name) " + "values (?, ?, ?, ?, ?, ?, ?)"; private string getbankaccountsql = "select banknr, sortcode, accountnumber, balance, interest, details, name " + "from accounts.bankaccounts 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) 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 getmaxbanknr() getter follows:
public int getmaxbanknr(){ try{ statement stmt = conn.createstatement(); resultset rs = stmt.executequery("select max(banknr) accounts.bankaccounts"); if(rs.next()){ maxbanknr = rs.getint(1); } }catch(sqlexception e){ printsqlexception(e); } return(this.maxbanknr); }
when this.maxbanknr = bankaccountdao.getmaxbanknr() called, wasn't returning updated maxbanknr. required new sql statement return proper value.
anyone using database list in same way me (i noticed quite few people same problem me while googling) should make sure loop really knows how many times loop.
i have posted answer, can't answer own question 8 hours, because don't have enough points.
hmm... tough one. think that if populatelist()
works itself, barefresbuttonactionperformed
not being executed itself.
i try regular actionevent
andactionperformed
listener or put kind of output in it, if barefreshbuttonactionperformed
, have show popup box saying override has been executed.
happy coding!
Comments
Post a Comment