简体   繁体   English

如何取消选择JTable中的一行?

[英]How can I deselect one row in JTable?

I have JTable with ListSelectionModel.MULTIPLE_INTERVAL_SELECTION: 我有ListSelectionModel.MULTIPLE_INTERVAL_SELECTION JTable:

 table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

When I use CTRL+mouse click, rows are selected and all are right. 当我使用CTRL +鼠标单击时,所有行均被选中。 But if I want to deselect only one selected row and press CTRL+mouse click to one selected row, all rows are deselected, not only one. 但是,如果我只想取消选择一个选定的行,然后按CTRL +鼠标单击以选中一个选定的行,则所有的行都将被取消选择,而不仅仅是一个。 Then if I press CTRL+mouse click to some row again, previouse rows are selected again, but without row, that I wanted to deselect. 然后,如果我再次按CTRL +鼠标单击某行,则会再次选择先前的行,但没有行,我想取消选择该行。 I want to deselect only one row using CTRL+mouse click. 我只想使用CTRL +鼠标单击取消选择一行。 How can I do that? 我怎样才能做到这一点?

EDIT: I had this in my code: 编辑:我的代码中有此:

table.getColumnModel().setColumnSelectionAllowed(true);

I found that if I remove this line then all works fine. 我发现如果删除此行,则一切正常。 But can JTable work correctly although 'ColumnSelectionAllowed' is true? 但是,尽管“ ColumnSelectionAllowed”为true,JTable能否正常工作?

Try method clearSelection on JTable . JTable上尝试方法clearSelection

it works by calling clearSelection on the ListSelectionModel 它通过clearSelection on the ListSelectionModel调用clearSelection on the ListSelectionModel来工作

If you are looking for deselection one row from many, use the below code: 如果要从许多中取消选择一行,请使用以下代码:

table.getSelectionModel().removeSelectionInterval(rowFrom, rowTo);

for deselecting one row: 用于取消选择一行:

table.getSelectionModel().removeSelectionInterval(row, row);

you can use multiple selection like this 您可以使用这样的多项选择

table.getSelectionModel().removeSelectionInterval(1, 3); //1,2,3 starting from 0
table.getSelectionModel().removeSelectionInterval(5, 5); //5

to add it back, use: 添加回去,使用:

table.getSelectionModel().addSelectionInterval(rowFrom, rowTo);

you can use multiple selection like this 您可以使用这样的多项选择

table.getSelectionModel().addSelectionInterval(1, 2); //1,2
table.getSelectionModel().addSelectionInterval(4, 4); //4

to set it to specific range, which will clear your old selection first, 将其设置为特定范围,这将首先清除您的旧选择,

table.getSelectionModel().addSelectionInterval(rowFrom, rowTo);

Which it is equal to: 它等于:

table.clearSelection();
table.getSelectionModel().addSelectionInterval(rowFrom, rowTo);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM