Java Swing JTable 以编程方式选择多行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15556341/
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
Java Swing JTable select programmatically multiple rows
提问by Anton Belev
I have a JTable with multiple rows and every row is presented via Point on a scatter plot. What I have to do is when a given point is selected on the scatter plot I have to associate this selection with selecting of the corresponding row in the JTable.
我有一个多行的 JTable,每一行都通过散点图上的 Point 呈现。我必须做的是,当在散点图上选择给定点时,我必须将此选择与选择 JTable 中的相应行相关联。
I have an Integer that represents, which row I have to highlight.
我有一个 Integer 代表,我必须突出显示哪一行。
What I tried is:
我试过的是:
JTable table = new JTable();
...
...// a bit of code where I have populated the table
...
table.setRowSelectionInterval(index1,index2);
So the problem here is that this method selects all rows in the given range [index1,index2]. I want to select for example rows 1,15,28,188 etc.
所以这里的问题是这个方法选择给定范围 [index1,index2] 中的所有行。我想选择例如第 1、15、28、188 行等。
How do you do that?
你是怎样做的?
回答by Russell Zahniser
To select just one row, pass it as both the start and end index:
要仅选择一行,请将其作为开始和结束索引传递:
table.setRowSelectionInterval(18, 18);
Or, if you want to select multiple, non-contiguous indices:
或者,如果要选择多个不连续的索引:
ListSelectionModel model = table.getSelectionModel();
model.clearSelection();
model.addSelectionInterval(1, 1);
model.addSelectionInterval(18, 18);
model.addSelectionInterval(23, 23);
Alternately, you may find that implementing your own subclass of ListSelectionModel
and using it to track selection on both the table and the scatterplot is a cleaner solution, rather than listening on the scatterplot and forcing the table to match.
或者,您可能会发现实现自己的子类ListSelectionModel
并使用它来跟踪表和散点图上的选择是一种更清晰的解决方案,而不是监听散点图并强制表匹配。
回答by Clerenz
It also works without using the ListSelectionModel:
它也可以在不使用 ListSelectionModel 的情况下工作:
table.clearSelection();
table.addRowSelectionInterval(1, 1);
table.addRowSelectionInterval(15, 15);
table.addRowSelectionInterval(28, 28);
...
Just don't call the setRowSelectionInterval, as it always clears the current selection before.
只是不要调用 setRowSelectionInterval,因为它之前总是清除当前选择。
回答by BullyWiiPlaza
Here is a generic method to accomplish this:
这是实现此目的的通用方法:
public static void setSelectedRows(JTable table, int[] rows)
{
ListSelectionModel model = table.getSelectionModel();
model.clearSelection();
for (int row : rows)
{
model.addSelectionInterval(row, row);
}
}
回答by Jabir
There is no way to set a random selection with a one method call, you need more than one to perform this kind of selection
无法通过一个方法调用设置随机选择,您需要多个方法来执行这种选择
table.setRowSelectionInterval(1, 1);
table.addRowSelectionInterval(15, 15);
table.setRowSelectionInterval(28, 28);
table.addRowSelectionInterval(188 , 188 );
And So On....
等等....