[英]make only one checkbox selectable in JTable Column
我有一個帶有2個coloumns的JTable,一列是對象類型,第二列是復選框。 在該表中,用戶只想選擇一個復選框。 我嘗試了很多代碼(例如: jtable復選框單選和第二個 ),最后我通過使用函數完成了這個。
這是我的功能:
public void setSelectionEnableOrDisable(JTable table) {
int earlierSelectionRow = 0;
int selectedRow = table.getSelectedRow();
for (int i = 0; i < table.getRowCount(); i++) {
if ((Boolean) table.getValueAt(i, 1) == true && i != selectedRow) {
earlierSelectionRow = i;
}
}
table.setValueAt(true, selectedRow, 1);
table.setValueAt(true, earlierSelectionRow, 1);
}
但是,這個問題是什么,當我慢慢點擊checbox時,它很好。 如果我快速點擊2或3個復選框,那么我的代碼允許多選。 這有什么不對?
我認為你可以做得更簡單,比如:
public void setSelectionEnableOrDisable(JTable table) {
int selectedRow = table.getSelectedRow();
if ((Boolean)table.getValueAt(selectedRow , 1)) {
for (int i = 0; i < table.getRowCount(); i++) {
if ( i != selectedRow) {
table.setValueAt(false, i, 1);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.