简体   繁体   English

在Swing中的jTable的第一行转移焦点

[英]Transfer Focus at first Row of jTable in Swing

I want to pass the focus to 1st row of JTable. 我想将焦点转移到JTable的第一行。 I can't use editCellAt(row,column) method because I don't want to allow user to change content of table. 我不能使用editCellAt(row,column)方法,因为我不想让用户更改表的内容。 changeSelection(row,column,false, false) method also does not work properly. changeSelection(row,column,false,false)方法也无法正常工作。

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. 如果我按ENTER键,控制应该在表的第一行。 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: 焦点(JTable作为组件)和选择(行/列)的两个概念不一定相关,如果你想要两个你必须调用单独的方法来达到两者:

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. 相反,它是column- rowselection模型和rowSelection模型的主角,它们通常具有比其他模型更粗/不同边框的视觉线索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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