JTable单元监听器? [英] JTable cell listener?
问题描述
我正在使用JTable.每当一个单元格时,我都需要收到通知 选择更改.我试图使用ListSelectionListener,但是 当行选择更改时,我只会收到通知.如果我选择一个 新列在同一行上,我不会收到通知.我要知道什么时候 单元格被选中,而不是在更改单元格时被选中.我可以使用一个侦听器吗?
I am using a JTable. I need to get a notification whenever a cell selection change. I tried to use the ListSelectionListener but I only get notification when the row selection change. If I select a new column on the same row, I don't get notify. I need to know when the cell is selected, not when the cell is changed. Is there a listener that I can use to do this ?
推荐答案
最简单的方法是调用setCellSelectionEnabled(true)
,并将对表的引用传递给侦听器.调用侦听器后,请在原始表上调用getSelectedRow()
和getSelectedColumn()
.
The easiest way to do this is to call setCellSelectionEnabled(true)
, and pass a reference to your table to the listener. When the listener is invoked, call getSelectedRow()
and getSelectedColumn()
on the original table.
另一种方法是在表上设置行选择侦听器,在ColumnModel
上设置列选择侦听器,然后找出它们的交集.
The alternative is to set a row selection listener on the table, a column selection listener on the ColumnModel
, and then figure out their intersection.
这篇关于JTable单元监听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!