How to get the number of columns and rows that are visible in my JList?

泪湿孤枕 提交于 2019-12-11 05:15:42

问题


I have a JList that is inside of a JscrollPane and I am trying to do some pagination on the data, in which as the user scrolls it loads the appropriate amount of data onto the screen. The problem is I need to figure out how much data I need to buffer out of my file and the only way to do that is to figure out how much data needs to be displayed on the screen. Does anyone have an idea of how to get the number or rows and columns that can be displayed out of my JList? Also is doing pagination with a JList and a JScrollPane a good idea or is there some other approach that would be better suited for displaying a ton of data in a scroll panel?

Thanks


回答1:


You can use getFirstVisibleIndex and getLastVisibleIndex methods of JList to calculate number of items it displays when visible.

If size of viewport in scroll pane changes then the number of visible rows will also change. So if the component is resizable you may need to listen to the change in the component size.




回答2:


Do getModel() on the JList to get the underlying ListModel, then call getSize() on it.

EDIT: oh, and if the model contents consist of a datastructure with multiple columns, call whatever suitable method exists on that for the size. You can get a row with getElementAt(int index). Might require some more work if the column count is variable.



来源:https://stackoverflow.com/questions/7893162/how-to-get-the-number-of-columns-and-rows-that-are-visible-in-my-jlist

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