Java Swing - 将行#列(行标题)添加到 JTable
我通过自定义表模型将数据库中的数据加载到 JTable 中。我想要一列(应该是第一列),它只显示显示行号(即它不与任何数据(或排序)相关联,而只是屏幕上开始的行号)于 1)。这些“行标题”应该像行标题一样显示为灰色。
知道如何做到这一点吗?
谢谢
I have data from a database loaded into a JTable through a custom table model. I want to have a column (should be the first column) which simply shows the display row number (i.e. it is not tied to any data (or sorting) but is simply the row number on the screen starting at 1). These "row headers" should be grayed out like the row headers.
Any idea how to do this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用什么表模型?
您可以重写
public Object getValueAt(int row, int column)
在 TableModel 中执行此操作。即,
如果在对 JTable 进行排序时这不起作用,则另一种解决方案是在自定义
TableCellRenderer
并覆盖:What TableModel are you using?
You can override
public Object getValueAt(int row, int column)
to do this in your TableModel.I.e.
If that isn't working when you sort your JTable, then another solution is to implement it in a custom
TableCellRenderer
and override:您可能正在寻找此页面: http://www .chka.de/swing/table/row-headers/JTable.html
This page might be what you're looking for: http://www.chka.de/swing/table/row-headers/JTable.html
如果您希望在进行水平滚动时行标题保持固定(如在 Excel 中),那么您可以将两个 JTable 合并在一起。该组件向您展示了它是如何完成的:
http://blue- walrus.com/2014/12/row-number-column-in-jtable/
If you want a row header that remains fixed in place when you do a horizontal scroll (like in Excel), then you could merge two JTables together. This component shows you how its done :
http://blue-walrus.com/2014/12/row-number-column-in-jtable/