0

I want to pass the focus to 1st row of JTable. I can't use editCellAt(row,column) method because I don't want to allow user to change content of table. changeSelection(row,column,false, false) method also does not work properly.

Please suggest me some way to transfer focus to first row.


I have textfield and table. Initially my control is at textfield. If I press ENTER,control should go at first row of table. I have provided the code below. Please help me out.

private void txtRawMaterial(java.awt.event.KeyEvent evt) //txtRawMaterial is textfield
{      
    if (evt.getKeyCode()==KeyEvent.VK_ENTER) 
    {
         if(table.getRowCount()>0)    //table is jTable
         {
                table.requestFocus();
         }
    }
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880

1 Answers1

2

The two concepts of focus (of the JTable in its role as component) and selection (of rows/columns) are not necessarily related, if you want both you'll have to invoke separate methods to reach both:

table.requestFocus();
table.changeSelection(0, 0, false, false);

Also note that the focused cell is not a focusOwner in the usual sense. Instead, it is the lead of both column- and rowSelection models that typically has a visual clue like a thicker/different border than the others.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • +1 for a the distinction. See also *UIManager Defaults*, cited [here](http://stackoverflow.com/a/3974748/230513). – trashgod Oct 02 '12 at 16:49