私は、最後の列(チェックボックスとして表示)にいくつかのブール値を持つカスタムDefaultTableModelを使ってJTableを持っています。setValueAtを使用して相互排他的なチェックボックスを再作成
クリックした値を取得するためにMouseListenerを追加すると、目盛りボックスのトグルが行われなくなったように見えます。
// As soon as this is used in the component
// that is using the JTable, the toggling stops
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
int col = table.getSelectedColumn();
int row = table.getSelectedRow();
Object o = table.getModel().getValueAt(row, col);
私は、イベントリスナーによって消費されていることを前提としています。トグル動作を復元するためにMouseListenerコードに何を追加できますか?
編集:
おっと、問題は私のオーバーライドであることが表示されます:
@Override
public void setValueAt(Object aValue, int row, int column) {
// Attempt at mutually exclusive checkboxes
if(column == 2){ // Starts at 0. Seek an alternative solution to avoid hardcoding?
// Algorithm: cycle through List to set other Booleans to false
// Uses entities. Is there another way of getting the number of rows from model?
List<MyEntity> myEntities = this.getDatas();
for(int i = 0; i < myEntities.size(); i++){
if(i != row){
// Make sure this calls parent
super.setValueAt(false , i, 2);
}
}
} else {
super.setValueAt(aValue, row, column); // Call parent class
}
}
あなたはテーブルセルから値を取得する必要がある理由、あなたの質問に本当の答えのためにあなたはhttp://sscce.org/ – mKorbel
@mKorbelを投稿する必要があります:私は投稿してみたいですいくつかの簡潔なコードがいくつかの依存関係があります。ブール値を表す列をクリック - >クリックされたブール値をtrueに設定(チェックボックスをオンにする) - >列内の他のすべてのブール値を相互排除のためにfalseに設定 - >チェックボックスの1つがチェックされている場合、JButtonを有効にします。 –
実際に実装する必要があるものが不明な場合は、非常に簡単な方法と1つ2つの最も複雑な方法がありますが、どれがMVCモデルに適しているかを確認してください。 – mKorbel