如何使jtable中的行不可选择? [英] how to make a row in jtable nonselectable?

查看:635
本文介绍了如何使jtable中的行不可选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次使用jtable.执行动作后,如何无法再次选择jtable中的特定行.我尝试了setRowSelectionAllowed(boolean)方法,但是它适用于所有行.

I am first time using jtable. How can I make a particular row in jtable cannot be selected again after an action is performed. I tried setRowSelectionAllowed(boolean) method, but it applied to all the rows.

推荐答案

将表选择模型设置为不允许选择禁止行的列表选择模型:

Set the table selection model to the list selection model that disallows selection of the forbidden rows:

class RestrictedSelector extends DefaultListSelectionModel {

  HashSet<Integer> forbiddenRows = new HashSet<Integer>();

  @Override
  public void addSelectionInterval(int index0, int index1) {
    for (int row = index0; row <= index1; row++) {
       if (forbiddenRows.contains(row)) {
         // You can also have more complex code to select still
         // valid rows here.
         return;
       }
    }
  }

 // Implement these in the same spirit:

 public void insertIndexInterval(int index0, int index1) 
 ...
 public void setSelectionInterval(int index0, int index1) 
 ...
 public void setLeadSelectionIndex(int leadIndex)      
 ...

 // and others, see below.
}

此处中检查所有可以使用的方法必须被覆盖.

Check here for all methods that must be overridden.

现在:

RestrictedSelector selector = new RestrictedSelector();

selector.forbiddenRows.add(NOT_THIS_ROW_1);
selector.forbiddenRows.add(NOT_THIS_ROW_2);

myTable.setSelectionModel(selector);

如果表中的行是可排序的,则可能还需要使用convertRowIndexToModelconvertRowIndexToView,因为必须禁止选择模型中而不是表中的行号.

If your table rows are sortable, you may also need to use convertRowIndexToModel and convertRowIndexToView as probably it is the row number in the model, not in the table, that must be banned from being selected.

这篇关于如何使jtable中的行不可选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆