在 Java 桌面应用程序中实现分页 SQL 结果表的有效方法
我需要从 SQL 选择结果集构建一个表。 我想向表添加一些分页功能,因为结果集可能非常大。 关于如何在 SQL 级别进行分页有很多讨论。 但是如何在GUI层面实现分页呢? 我有两个想法:
- 以 Web UI 风格进行分页 - 例如谷歌搜索结果分页;
- “Excel 样式”- 当用户向下滚动时,表格所在的滚动窗格会展开。
第二个看起来更好,但实现起来很复杂。 何时对下一个数据块执行 SQL 选择,以便用户无需等待。 应该有一些“预读”逻辑? 如果用户滚动得很快会发生什么? 如何处理不再可见的行以保持恒定的内存使用量? 也许已经有这样的表格组件或很好的例子? 或者也许这个漂亮的表格功能不值得努力实现它?
I need to build a table from SQL select result set. I want to add some paging functionality to the table because the result set can be very large. There was many discussions how to do the paging at the SQL level. But how to implement the paging at the GUI level? I have two ideas:
- Do the paging in a web UI style - for example google search result paging;
- "Excel style" - the scroll pane where the table resides expands as user scrolls down.
The second one looks nicer but complex to implement. When to do SQL select for a next chunk of data so that a user haven't wait for it. There should be some "read ahead" logic? What will happen if user scrolls quite quickly? What to do with rows that aren't visible any more to have constant memory usage?
Maybe there is already such a table component or good examples? Or maybe this good looking table functionality is unworthy of efforts to implement it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以直接在 TableModel 中执行所有操作,无需更改 UI 中的某些内容(您可能会在 UI 中显示某些内容 - 状态 - 但您不需要更改 JTable 呈现自身的方式)。
只需实现一个 JDBCTableModel 即可:
分页对于 Swing 应用程序没有意义,它是在当前精美的 AJAX 库实现表格滚动之前发明的(请参阅 http://www.ext-livegrid.com/)。
You could do everything directly into your TableModel, there is no need to change something in the UI(well you migh show something in the UI - a status - but you don't need to change how JTable renders itself).
Just implement a JDBCTableModel which will have :
Paging doesn't makes sense for a Swing application, it was invented before the current fancy AJAX libraries could implement table scrolling(see http://www.ext-livegrid.com/).
为 AJAX 库构建分页表的人显然已经对此进行了很多思考,因此如果您找不到已经构建的东西,您也许可以向他们学习。 由于您使用的是 Java,请注意,您可以找到用 Java 编写的 GWT 分页表。 请参阅以下 SO 线程:
GWT 分页小部件
GWT 中的最佳可分页表实现
您的实现的想法(以及大部分代码)可以在提到的开源项目中找到。
The people who have built paging tables for AJAX libraries have obviously thought about this a lot, so you may be able to learn from them if you can't find something already built. Since you are using Java, note that you can find paging tables written in Java for GWT. See these SO threads:
GWT paging widget
Best pageable table implementation in GWT
The ideas (and much of the code) for your implemenation may be found in the open source projects mentioned.