JTable 中的页脚行
将页脚行放入 JTable 的最佳方法是什么? 有人有任何示例代码来执行此操作吗?
到目前为止,我想到的唯一方法是将一个特殊的行放入表模型中,该行始终排序到底部。
这就是我最终得到的结果:
JTable mainTable = new JTable(mainTableModel);
JTable footerTable = new JTable(footerModel);
footerTable.setColumnModel(mainTable.getColumnModel());
// Disable selection in the footer. Otherwise you can select the footer row
// along with a row in the table and that can look quite strange.
footerTable.setRowSelectionAllowed(false);
footerTable.setColumnSelectionAllowed(false);
JPanel tablePanel = new JPanel();
BoxLayout boxLayout = new BoxLayout(tablePanel, BoxLayout.Y_AXIS);
tablePanel.setLayout(boxLayout);
tablePanel.add(mainTable.getTableHeader()); // This seems like a bit of a WTF
tablePanel.add(mainTable);
tablePanel.add(footerTable);
排序工作正常,但选择页脚行有点奇怪。
What is the best way to put a footer row into a JTable? Does anyone have any sample code to do this?
The only approach I've thought of so far is to put a special row into the table model that always get sorted to the bottom.
Here is what I ended up with:
JTable mainTable = new JTable(mainTableModel);
JTable footerTable = new JTable(footerModel);
footerTable.setColumnModel(mainTable.getColumnModel());
// Disable selection in the footer. Otherwise you can select the footer row
// along with a row in the table and that can look quite strange.
footerTable.setRowSelectionAllowed(false);
footerTable.setColumnSelectionAllowed(false);
JPanel tablePanel = new JPanel();
BoxLayout boxLayout = new BoxLayout(tablePanel, BoxLayout.Y_AXIS);
tablePanel.setLayout(boxLayout);
tablePanel.add(mainTable.getTableHeader()); // This seems like a bit of a WTF
tablePanel.add(mainTable);
tablePanel.add(footerTable);
Sorting works fine but selecting the footer row is a bit strange.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尝试使用第二个 JTable,它使用与数据表相同的列模型,并将页脚数据添加到该表中。 在原始表格下添加第二个(页脚)表格。
Try using a second JTable that uses the same column model as your data table and add your footer data to that table. Add the second (footer) table under your original table.
看起来这个 项目 有一个名为 JideScrollPane 的组件,它宣传对行页脚的支持。 我自己没有尝试过,但听起来它正是你想要的!
该网站还有一个演示应用程序,您可以在其中看到它的运行情况,而且看起来相当不错。
请注意,他们的很多东西似乎都需要付费,但他们的 JideScrollPane 看起来是免费且开源的。
Looks like this project has a component called JideScrollPane which advertises support for a row footer. I haven't tried it myself, but it sounds like it does exactly what you want!
The website also has a demo app where you can see it in action and it that looks pretty good.
Note that it seems a lot of the their stuff you have to pay for, but their JideScrollPane looks to be free and open source.
使用两个位于彼此下方的表是一个好方法。
如果您希望能够调整大小/移动/删除列,关键是不要在表之间重用相同的列模型。 让听众调整大小。 参见示例:
Using 2 tables below each-other is a good approach.
If you want to be able to resize/move/remove the colums, key is NOT to reuse the same columnModel between the tables. Have a listener do the resizing. See example:
我唯一一次这样做时,我只是在模型中添加了一行,如下所示:
_tableContents 当然是模型背后的实际数据。 当然,您必须注意模型中的额外行(在 setValueAt(...) 等调用中)
祝您好运。
The only time I have done this I just added a row in the model like so:
_tableContents is of course the actual data behind my model. You'll have to be aware of the extra row in the model of course (in such calls as setValueAt(...))
Good luck.
您可以尝试实现自己的 TableCellRenderer 用页脚替换最后一个可见行的渲染内容。 然而,这不会固定在表格的底部,当您滚动时它可能会上下移动。
You could try implementing your own TableCellRenderer that replaces the rendered content of the last visible row with your footer. However this wouldn't be fixed at the bottom of the table, it will likely shift up and down as you scroll.
我想最好的方法(但肯定不是最简单的)是查看 JTableHeader 组件的源代码,了解它是如何工作的,然后创建您自己的 JTableFooter > 组件。 您可以将
JTableHeader
UI Delegate 重新用于页脚,我认为主要区别在于getHeaderRect()
方法,它确定给定列的边界标题图块。I guess the best approach (but certainly not the easiest) would be to take a look at the source code for the
JTableHeader
Component, see how it works and then create your ownJTableFooter
Component. You can re-use theJTableHeader
UI Delegate for the footer, I think the main differences would be in thegetHeaderRect()
method, where it determines the bounds of a given column header tile.这是java bug数据库中提到的另一个解决方案
Here is another solution mentioned in the java bug database