java JTable 单元格侦听器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12376826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:38:37  来源:igfitidea点击:

JTable cell listener?

javaswingjtablelistselectionlistener

提问by tadpole

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 ?

我正在使用 JTable。每当单元格选择更改时,我都需要收到通知。我尝试使用 ListSelectionListener 但我只在行选择更改时收到通知。如果我在同一行上选择一个新列,我不会收到通知。我需要知道何时选择单元格,而不是何时更改单元格。是否有我可以用来执行此操作的侦听器?

采纳答案by parsifal

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.

最简单的方法是调用setCellSelectionEnabled(true),并将对表的引用传递给侦听器。当侦听器被调用时,在原始表上调用getSelectedRow()getSelectedColumn()

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.

另一种方法是在 table 上设置一个行选择侦听器,在 上设置一个列选择侦听器ColumnModel,然后找出它们的交集。

回答by kleopatra

One way to receive notification on columnselection changes - as already answered by @parsifal(in the comments - is to grab the TableColumnModel's internal selectionModel and register a listener:

接收选择更改通知的一种方法- 正如@parsifal(在评论中)已经回答的那样 - 是获取 TableColumnModel 的内部 selectionModel 并注册一个侦听器:

table.getColumnModel().getSelectionModel().addListSelectionListener(selectionListener);

Another way is to register a TableColumnModelListener with the columnModel:

另一种方法是使用 columnModel 注册一个 TableColumnModelListener:

table.getColumnModel().addColumnModelListener(columnModelListener);

The first is "shorter" in terms of code: just one method to implement vs. several - most empty except the columnSelectionChanged.

第一个是在代码方面“更短”:只有一种方法可以实现与几种方法相比 - 除了 columnSelectionChanged 之外,大多数都是空的。

The second is more robust against dynamic changes: with the first there is no possibility to guard against changes of the selectionModel property of the columnModel ... because it is not a property. Or in other words: in the (concededly rare) case that application code swaps out the selectionModel the listener is listening to the Void. Installing a columnModelListener is immune against such a change, as the columnModel passes on the events from its selectionModel whichever it would be.

第二个对动态变化更健壮:第一个不可能防止 columnModel 的 selectionModel 属性的变化......因为它不是一个属性。或者换句话说:在应用程序代码换出 selectionModel 的情况下(确实很少见),侦听器正在侦听 Void。安装 columnModelListener 不受此类更改的影响,因为 columnModel 从它的 selectionModel 传递事件,无论它是什么。