2013-03-23 84 views
1

我第一次使用jtable。我如何使jtable中的某一行在执行操作后不能再被选中。我尝试了setRowSelectionAllowed(boolean)方法,但它应用于所有行。如何在jtable中选择一行不可选?

+0

调用JTable和TableModel的伟大javadoc,所有应该显示! – Thihara 2013-03-23 11:26:51

+0

提示:isCellEditable – Thihara 2013-03-23 11:27:31

+1

@Thihara也可以选择不可编辑的单元格。 – h22 2013-03-23 11:38:28

回答

1

设置表的选择模型的列表选择模式,禁止禁行的选择:

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. 
} 

检查here为必须覆盖所有方法。

现在:

RestrictedSelector selector = new RestrictedSelector(); 

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

myTable.setSelectionModel(selector); 

如果你的表行排序,您可能还需要使用convertRowIndexToModelconvertRowIndexToView因为可能它是在模型中的行数,未在表中,必须从禁止被选中。