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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:20:05  来源:igfitidea点击:

How to programmatically deselect the currently selected row in a JTable (swing)?

javaswing

提问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 JTablecalled clearSelection. This, in turn calls clearSelectionon the ListSelectionModelof the table and the column model.

有一个方法JTable叫做clearSelection. 这反过来要求clearSelectionListSelectionModel表和列模式。

回答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 只是有一些快捷方式。