.getSelectedRow() is returning -1

前端 未结 2 496
悲哀的现实
悲哀的现实 2021-01-15 04:04

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 ind

相关标签:
2条回答
  • 2021-01-15 04:47

    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() would not work with multiple selected rows (multiple selections allowed), as it will always return "the index of the first selected row".

    0 讨论(0)
  • 2021-01-15 04:57

    have you tried to put e.consume(); as the last statement?

    public void mouseClicked(MouseEvent e){
     if(e.getModifiers() == MouseEvent.BUTTON1_MASK){
       if(e.getClickCount() == 2){
         int selRow = myTaskTable.getSelectedRow();
         System.out.println("GridReport double clicked on row="+selRow);
         e.consume();
        }
      }
    }
    

    normaly e.consume(); is called when you are done with your reactive code. This clears dependencies of the Event, so it might also clear the selected Row.

    0 讨论(0)
提交回复
热议问题