• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reloading JLIST  RSS feed

 
Steve Dunn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm building a JLIST from a JDBC query and I want to be able to reload the list whenever the Access table I built the list from is is updated (records added or deleted). I'm able to build the initial list just fine using the following method; which I also use to reload the list when and add or delete event occurrs. However, the list doesn't seem to reload. Can someone tell me what I'm doing wrong?

Thanks for your help,


private void loadScrollPanel() {

vBookCodes = new Vector();
titleList.setModel(new DefaultListModel());

DefaultListModel dlm = (DefaultListModel) titleList.getModel();

try{
BookDB.connect();
rsBooks = BookDB.open();

while(rsBooks.next()) {
dlm.addElement(rsBooks.getString("BookTitle"));
vBookCodes.add(rsBooks.getString("BookCode"));
}

BookDB.close();

titleList.setModel(dlm);

titleScroll.setViewportView(titleList);
}

catch (ClassNotFoundException e){
JOptionPane.showMessageDialog(null, "cnf loadScrollPane " + e.getMessage());
System.exit(1);
}

catch (SQLException e){
JOptionPane.showMessageDialog(null, "sql loadScrollPane " + e.getMessage());
}
}
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me that your JList should show
any new books at the bottom of the list, so
I'm not sure what's going wrong.

Two comments:

1) <snip> (This comment about vBookCodes removed
because it was dumb, sorry. But now I wonder if perhaps
your code might be keeping a reference to the old Vector
when vBookCodes is set to refer to the new one.)

2) The call to titleList.setModel(dlm) should not
be necessary. DefaultListModel.addElement() sends
an event to its listeners, and titleList is presumably
a listener.
[ December 12, 2005: Message edited by: Brian Cole ]
 
Steve Dunn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use the vector as part of the ListSelectionListner to identify the book code for a selected book title in the list, then query the book table for the selected book and load to text fields for update.

JList "titleList" only has the ListSelectionListner added. I have ActionListner on buttons I use to identify when an ADD or DELETE occurs then call method loadScrollPanel.
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by intravler:
JList "titleList" only has the ListSelectionListner added.


Right, but the JList autmatically adds itself as a listener
to its model. That's how it updates when the model changes.

Sorry--wasn't trying to be confusing.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, intravler . Please revise your name to meet the guidelines in the JavaRanch naming policy. We ask that you use a real first and last name so we can maintain a friendly environment.

Thanks,

- David
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!