2013-03-23 83 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因爲可能它是在模型中的行數,未在表中,必須從禁止被選中。