Can't navigate a JTable by the arrow keys after using setRowSelectionInterval(int, int)
I'm extending JTable and I have a populateData() method that I'm using to load data into the table model. In this method I'm using:
setRowSelectionInterval(tableRow, tableRow);
And it is working as I want it to, which is to highlight the first row. But there is one problem: I cannot move the selection to the next row by the down arrow key. I have to click on any row (even the one that is highlight开发者_运维知识库ed) with the mouse to be able to navigate by the arrow keys.
void populateData(Collection<Book> b) {
myModel.populateData(b);
if (myModel.getRowCount() > 0) {
setRowSelectionInterval(0, 0);
}
}
Note: I'm enabling only row selections. Column and cell selections are disabled.
Any ideas how to fix this?
Thanks in advance.
I would guess focus in not on the table so it doesn't react to key presses. You may need to add:
table.requestFocusInWindow();
If this doesn't work the instead of setRowSelectionInterval(...) try using:
table.changeSelection(0, 0, false, false);
精彩评论