Increase performance of JCombobox with many entries

给你一囗甜甜゛ 提交于 2019-12-11 04:38:22

问题


I hope this question is SO worthy, but I'll give it a try...

I have a rather complex GUI and am looking to increase the overall performance a bit. I stumbled upon some comboboxes that are populated with a lot of entries (up to 10000 lines). The creation of all swing elements is already optimized, so they are normally initialized only once. But it seems kinda memory intensive to have, let's say, 10 combobox models with 10k entries always in the background.

I have implemented a search feature, so the user can type 'B' and the list jumps to the first entry starting with 'B' (and further refining if more chars are added). But this does not alter the model, just reset the selected index, so the list still contains all entries.

My question is:

Are there any best practices on how to handle a lot of entries within a combobox? And from the user's view, would you rather display all entries, or just the first 100 and others on demand after a key was pressed?

Or to ask a more specific question:

Is it better to keep a big comboboxmodel in memory, or to create a small one (with ~100 entries) every time the user enters a new key?

Thanks for input and suggestions!


回答1:


I would add e.g first 100 and one more item "More..." or "Show All". When user clicks on the item all the 10k records are loaded. Alternatively if user starts typing I would subtract the suitable range (but again not more than 100) and show them in the list.




回答2:


10000 entries should not be displayed in a JComboBox. You may use a JTextField with autocompletion. Swingx provides an utility to do this: AutoCompleteDecorator.

You could also display the data in a JXList (from swingx again) and used a JTextField to filter its content.

JList is a component already optimized. Only the displayed data (visible in the scrollpane viewport) are really painted. And for memory optimization the flyweight pattern is used for the cell rendering, there is only one ListCellRendeter for drawing all the cells.

If for some reasons, you have to use a JComboBox. Note that the JCombobox uses a JList to display the data on a Popmenu, so it gets the benefits explained above.

For memory improvement of your model, you may consider to load lazily the data. But it is a bit complicated since you want also autocompletion features. Your autocompletion will have to seacrh in the persisted data.




回答3:


How much ram is your application using? does it take too long to load initially?

Desktop/notebook memory is dirt cheap nowadays. If your application is a dedicated/professional one, aka the user will be using it intensively in the foreground, you want to offer your user speed and responsiveness above everything. If it were something like an instant messaging tool, you would want to keep its RAM fingerprint low.

My advice is to keep everything in memory, provided a modest 2012 computer build can handle it.

Edit: from a usability point of view, StanislavL's suggestion appeals to me as well.




回答4:


With so many items a ComoBox is not suitable. Your idea of smart search is good, but you should improve it by not prepopulating the CB, but rather load the data asynchrounously while the user is typing and start at least 1 or 2 characters.



来源:https://stackoverflow.com/questions/11983798/increase-performance-of-jcombobox-with-many-entries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!