Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JComboBox for large number of list items  RSS feed

 
alex earnshaw
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm using a JComboBox to display a very large number of items (>1000). As you can imagine it is not very responsive, as the loop for adding the items is taking a long time to complete. I have tried adding the items in a seperate thread which improves things a lot, but then actually scrolling through and selecting then items is still very slow.
What I'm wondering is there an alternative swing component which may suit this job better, e.g. a searchable list or something?
Any help appreciated,
Alex
 
Joe Vigor
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A quick way to add a list of items to a JComboBox is through the Model. You can add a list of items as a Vector or Object array. Although it won't help the performance of scrolling, it will load very quick.
i.e. (assume items is a Vector or Object[] that holds your list)
DefaultComboBoxModel cbm = new DefaultComboBoxModel(items);
comboBox1.setModel(cbm);
 
alex earnshaw
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joe, that's certainly very useful to know!!
I'm still hoping that someone knows of an alternative to JComboBox which would serve my purpose much better? Anyone?
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the purpose. You have explained the problem but not the underlying requirements.
 
alex earnshaw
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically I am just retrieving a list of items (processed file names) from a database, and the user needs to ba able to select one of the items retrieved. However the list of items in the database grows daily, so eventually it will get very big. Therefore I need some way that the user can browse or search a list of items easily regardless of it's size.
I have considered restricting the size of the list retrieved by date, but the user wants all the items available to him in some way, regardless of the date they were stored. Hope that makes things clearer.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could just display x number in the list at a time and use foward and back buttons to navigate. That way it doesn't matter that the overall number grows. Just a thought.
 
alex earnshaw
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Stevens:
You could just display x number in the list at a time and use foward and back buttons to navigate. That way it doesn't matter that the overall number grows.

That sounds just the job thanks! Would I be heading in the right direction by looking at a JList and DefaultListModel to do this? An example would be great if you know of any...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!