确定单击了哪个 JTable 单元格
当用户单击JTable
上的单元格时,如何计算出单击的单元格的行和列?我如何在 JLabel
中显示此信息?
When a user clicks a cell on a JTable
, how do I figure out the row and column of the clicked cell? How would I show this information in a JLabel
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
现有的答案有效,但如果您不启用单元格选择,还有一种替代方法可能效果更好。在您的
MouseListener
中,执行如下操作:The existing answer works, but there is an alternate method that may work better if you're not enabling cell selection. Inside your
MouseListener
, do something like this:您可以在 JTable 上使用以下方法来检索所选单元格的行和列:
并向表添加一个
SelectionListener
以捕获选择表时的事件。You can use following methods on JTable to retrieve row and column of the selected cell:
And add a
SelectionListener
to table to catch the event when the table is selected.它对我有用!
It is working for me!!!
你尝试过
addMouseListener()
吗?我希望您正在使用 Swing 的 JTable。did you try
addMouseListener()
? I hope you are about using Swing's JTable.我发现当列被隐藏/重新排序时
columnAtPoint
返回可见的列索引,这不是我需要的。对我有用的代码是I've found that when columns are hidden/reordered
columnAtPoint
returns the visible column index, which isn't what I needed. Code which worked for me is