Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Reloading JLIST

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
}
}
 
Author
Posts: 974
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 974
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic