Java Swing JTable以编程方式选择多行 [英] Java Swing JTable select programmatically multiple rows
问题描述
我有一个包含多行的JTable,每行通过散点图上的Point表示.我要做的是,当在散点图中选择了给定点时,我必须将此选择与选择JTable中的相应行相关联.
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.
我有一个整数,代表我必须突出显示的行.
I have an Integer that represents, which row I have to highlight.
我尝试过的是:
JTable table = new JTable();
...
...// a bit of code where I have populated the table
...
table.setRowSelectionInterval(index1,index2);
因此,这里的问题是该方法选择了给定范围[index1,index2]中的所有行.我想选择例如1,15,28,188等行.
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.
你怎么做到的?
推荐答案
要仅选择一行,请将其作为开始索引和结束索引进行传递:
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);
或者,您可能会发现实现自己的ListSelectionModel
子类并使用它来跟踪表和散点图上的选择是一种更清洁的解决方案,而不是侦听散点图并强制表进行匹配.
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.
这篇关于Java Swing JTable以编程方式选择多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!