jtableを初めて使用します。アクションが実行された後、jtable の特定の行を再度選択できないようにするにはどうすればよいですか。setRowSelectionAllowed(boolean)
メソッドを試しましたが、すべての行に適用されました。
1 に答える
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.
}
オーバーライドする必要があるすべてのメソッドについては、こちらを確認してください。
今:
RestrictedSelector selector = new RestrictedSelector();
selector.forbiddenRows.add(NOT_THIS_ROW_1);
selector.forbiddenRows.add(NOT_THIS_ROW_2);
myTable.setSelectionModel(selector);
テーブルの行がソート可能である場合は、選択を禁止する必要があるのは、おそらくテーブルではなくモデルの行番号であるため、使用する必要がある場合もありconvertRowIndexToModel
ますconvertRowIndexToView
。
于 2013-03-23T11:34:30.237 に答える