当按下 Enter 键时,如何确定选择了 JTable 的哪一部分?
我有一个JTable
。我想知道当用户按 Enter 时选择了哪行和哪列。我怎样才能得到这些信息?
I have a JTable
. I want to know which row and column are selected when the user presses Enter. How can I get this information?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实现 TableModelListener。 tableChanged() 中的 TableModelEvent方法将告诉您更改的来源是哪行和哪列。
Implmenent a TableModelListener. The TableModelEvent from the tableChanged() method will tell you what row and column was the source of the change.
所有 Swing 组件都使用操作来处理击键。 Enter 键的默认操作是将单元格选择向下移动一行。如果您想更改此行为,则需要将默认操作替换为自定义操作。
查看按键绑定,了解如何替换操作的简单说明。
All Swing components use Actions to handle key strokes. The default Action for the Enter key is to move the cell selection down one row. If you want to change this behaviour then you need to replace the default Action with a custom Action.
Check out Key Bindings for a simple explanation on how to replace an Action.
将其添加到您的表中。对于
rowClicked
和colClicked
有两个int
全局变量。应该可以了如果您谈论使用键盘来注册事件,您必须找到选定的单元格,然后向其添加一个
KeyListener
。您可以使用以下代码来查找所选单元格。请注意,这实际上取决于单元选择模式。Add this to your table. Have two
int
globals forrowClicked
andcolClicked
. Should be good to goIf you talking about using the keyboard to register the event, you must find the selected cell, then add a
KeyListener
to it. You can use the following code to find the selected cell. Note that it really depends on the cell selection mode.