[英]Reading the Data of a Highlighted Jtable Row
我有一個帶有數據行的JTable
我有一個事件,每次單擊鼠標時都會監聽
private void tablePOMouseClicked(java.awt.event.MouseEvent evt) {
try {
int row1 = tablePO.getSelectedRow();
cellA = tablePO.getValueAt(row1, 0).toString();
cellB = tablePO.getValueAt(row1, 1).toString();
cellC = tablePO.getValueAt(row1, 2).toString();
cellD= tablePO.getValueAt(row1, 3).toString();
cellE = tablePO.getValueAt(row1, 4).toString();
cellF = tablePO.getValueAt(row1, 5).toString();
cellG = tablePO.getValueAt(row1, 6).toString();
cellH = tablePO.getValueAt(row1, 7).toString();
} catch (Exception e) {
}
}
變量cellA-H都是字符串。
它的工作正常,但是現在我要更改它,我不希望用戶需要使用鼠標,因此,我希望用戶僅使用向上/向下箭頭選擇該行來導航行並放入選中的行突出顯示在突出顯示下,但是我不知道如何實現,通過使用UP / DOWN鍵(不是通過單擊鼠標指向該行)從突出顯示/選定的行中讀取數據。
將ListSelectionListener
添加到表中。
無論您使用鼠標還是鍵盤,只要行選擇發生更改,都會生成一個事件。
閱讀Swing教程中有關如何編寫ListSelectionListener的部分, 以獲取更多信息和工作示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.