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:
我正在扩展JTable,并且有一个populateData()方法,我使用它将数据加载到表模型中。在这个方法中,我使用:
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 highlighted) 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.
提前谢谢。
1 个解决方案
#1
2
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:
如果这不能工作,而不是setRowSelectionInterval(…)尝试使用:
table.changeSelection(0, 0, false, false);
#1
2
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:
如果这不能工作,而不是setRowSelectionInterval(…)尝试使用:
table.changeSelection(0, 0, false, false);