Java 如何以编程方式取消选择 JTable (swing) 中当前选定的行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1019062/
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
How to programmatically deselect the currently selected row in a JTable (swing)?
提问by alves
I want to programmatically deselect the currently selected row (or rows) in a JTable.
我想以编程方式取消选择 JTable 中当前选定的行(或行)。
Basically I want the opposite of this:
基本上我想要相反的:
JTable table = ...;
table.setRowSelectionInterval(x,x);
I tried (with little hope) using:
我尝试(希望渺茫)使用:
table.setRowSelectionInterval(-1,-1)
or
或者
table.setRowSelectionInterval(1,0)
but it doesn't work.
但它不起作用。
采纳答案by Avrom
There is a method on JTable
called clearSelection
. This, in turn calls clearSelection
on the ListSelectionModel
of the table and the column model.
有一个方法JTable
叫做clearSelection
. 这反过来要求clearSelection
对ListSelectionModel
表和列模式。
回答by Steve Landey
I believe you can use this:
我相信你可以使用这个:
table.getSelectionModel().clearSelection().
The SelectionModel is what actually handles the selection. JTable just has a few shortcuts.
SelectionModel 是实际处理选择的对象。JTable 只是有一些快捷方式。