java swing jtable,Java Swing 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.

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.

How do you do that?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值