简体   繁体   中英

Transfer Focus at first Row of jTable in Swing

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();
         }
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM