< JTable> .getSelectedRow()返回-1 [英] <JTable>.getSelectedRow() is returning -1
问题描述
我在 JTable
上写了一个双击事件。我的JTable,即。 myTaskTable填充了许多具有多列的行。我希望在双击 JTable
的其中一行时检索行索引。我不知道为什么它总是返回-1的索引导致异常。我忽略了什么吗?可能出现什么问题?
I have written a "double-click" event on my JTable
. My JTable, viz. myTaskTable is populated with a number of rows having multiple columns. I want the row index to be retrieved when I double click on one of the rows of the JTable
. I am not sure why is it always returning me an index of -1 resulting in an exception. Am I am overlooking something? What could be going wrong?
这就是我从 JTable
中检索所选行的索引的方法 - myTaskTable
This is how I am retrieving the index of the selected row from the JTable
- myTaskTable
int selRow = myTaskTable.getSelectedRow();
谢谢!
编辑
以下是代码:
...
myTaskTable.addMouseListener(this);
...
public void mouseClicked(MouseEvent e)
{
if(e.getModifiers() == MouseEvent.BUTTON1_MASK)
{
if(e.getClickCount() == 2)
{
e.consume();
int selRow = myTaskTable.getSelectedRow();
System.out.println("GridReport double clicked on row="+selRow);
}
}
}
推荐答案
使用事件获取行索引,而不是表选择:
Get the row index using the event, not the table selection:
final int selectedRowIndex = table.rowAtPoint(mouseEvent.getPoint());
// If the rows are sorted or filtered
final int modelRowIndex = table.convertRowIndexToModel(selectedRowIndex);
getSelectedRow()
不适用于多个选定的行(允许多个选择),因为它将始终返回第一个选定行的索引。
getSelectedRow()
would not work with multiple selected rows (multiple selections allowed), as it will always return "the index of the first selected row".
这篇关于< JTable> .getSelectedRow()返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!