2013-04-03 28 views
0

我有一個應用程序在用戶按下按鈕時將「圖層」添加到JTable。應用程序在創建新文件時添加第一層。我希望在JTable中自動選擇這個圖層。有誰知道如何做到這一點?在JTable中默認選擇一個項目

回答

1

您可以通過JTable的選擇模型來控制選擇。

myJTable.getSelectionModel().setSelectionInterval(index, index); 
+0

謝謝你,偉大工程 – Pedrom 2013-04-03 19:47:04

0

簡單情況:

//if needed set selection model 
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
//at first, select row 
table.getSelectionModel().setSelectionInterval(from, to); 
//then select column if needed 
table.setColumnSelectionInterval(from, to) 
//at the end request focus 
table.requestFocusInWindow(); 

相當多難治,你有表,允許多行選擇:

table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 
//select needed lines 
ListSelectionModel model = table.getSelectionModel(); 
model.clearSelection(); 
int[] focusedLines = new int[] {0, 4, 10}; 
for(int actualRow : focusedLines) 
{ 
    model.addSelectionInterval(actualRow, actualRow); 
} 
//then select column if needed 
table.setColumnSelectionInterval(from, to) 
//at the end request focus 
table.requestFocusInWindow();