[英]JTable if any row is selected
我試圖弄清楚如何通過“if”語句向JTable添加邏輯,該語句檢查是否選擇了任何行。 我知道如何檢查是否選擇了特定行,但我似乎無法弄清楚如何檢查所有行。
if(tbl.isRowSelected(0)){
顯然,這是檢查特定行。
我也試過類似的東西
if(tbl.isRowSelected(0-2000)){
這不起作用,我也不希望它起作用。
這樣做的原因是我正在設置表,以便當用戶單擊一行然后點擊“編輯”按鈕時,將出現第二個表,其中包含與所選行相關的更多數據。 (這里使用哈希映射中的2D數組變得復雜,但我首先需要解決這個簡單的問題)。
我在這里先向您的幫助表示感謝!
這應該可以使用ListSelectionModel
,可以使用JTable::getSelectionModel()
檢索
因此,您可以調用table.getSelectionModel().isSelectionEmpty()
來查找是否選擇了任何行。
采用 :
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event) {
// do some actions here, for example
// print first column value from selected row
System.out.println(table.getValueAt(table.getSelectedRow(), 0).toString());
}
});
使用table.getSelectedRow()它返回所選行的索引,getSelectedColumn()返回所選列
並且有getSelectedRows()它返回所選行的索引數組
int[] indexs=table_name.getSelectedRows();
//all the selected row are here no need to go throw
//all your rows to see if they are selected or not
for(int index_row:rows){
//you code here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.