Java 选择多行 JTable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2141273/
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-13 04:03:47  来源:igfitidea点击:

Selecting multiple rows of JTable

javaswingjtable

提问by Supereme

I'm currently using JTable to show the contents in database. I want to provide the facility for user so that he can select the number of rows he wants using shift+arrow key and then later on delete those records using the option provided for deletion. Please provide a small example.

我目前正在使用 JTable 来显示数据库中的内容。我想为用户提供便利,以便他可以使用 shift+arrow 键选择他想要的行数,然后使用提供的删除选项删除这些记录。请提供一个小例子。

采纳答案by Roman

You need to allow multiple selection:

您需要允许多项选择:

table.setRowSelectionAllowed(true);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

Then you need to write appropriate selection listener. It's a bit harder, try to find in google related solutions. You can look at an example of selection listener.

然后您需要编写适当的选择侦听器。有点难,尝试在google中找到相关解决方案。您可以查看选择侦听器的示例