2012-05-23 24 views
1

我有一個GUI,它提供了一個包含數千行和幾列的表。我試圖通過在客戶端使用我的搜索功能時在GUI中找到的一些匹配進行迭代。我可以選擇行來突出顯示一個匹配,但我不知道使用什麼方法來移動GUI窗口的焦點以移動到下一個查找。在Java中設置GUI窗口焦點行

在下面的代碼中,my_model被實例化爲一個擴展AbstractTableModel的簡單類,而my_table是一個標準的JTable。無論何時找到匹配,我都希望能夠將選定的時間間隔設置爲下一個查找,並且我還想要移動窗口焦點以在下一個查找中。

int i, ln; 
    for (i = 0, ln = my_model.getRowCount(); i < ln; i++) 
    { 
     String cur_entry = (String)my_model.getValueAt(i, 1); 

     if (! cur_entry.contains(search_query)) continue; 

     my_table.setRowSelectionInterval(i, i); 

     // This is where I'd like to focus the GUI window on row i. 


     String options[] = new String[]{"Find Next","Done"}; 
     String initialValue = options[0]; 

     int code = JOptionPane.showOptionDialog(null, "Continue Searching", "Find", 0, JOptionPane.QUESTION_MESSAGE, null, options, initialValue); 

     if (code != 0) 
      return; 

    } 

讓我知道是否需要任何進一步的信息。任何幫助將不勝感激。

回答

1

使用此:

my_table.scrollRectToVisible(my_table.getCellRect(i, 1, true)); 
1

試試這個,

row.requestFocusInWindow();