简体   繁体   English

单击JTable时丢失和获得文本字段焦点

[英]Text Field Focus lost and Gained when clicked on JTable

I have a textfield that when gets Focus shows a panel with a table and when Focus lost makes it invisible. 我有一个文本字段,当获取焦点时显示带有表格的面板,而焦点丢失时使其不可见。

Typing into the field searches the database alongside and displays the results in table. 在该字段中键入将在数据库旁边搜索并在表中显示结果。

When I click on the table to select a row, the text field loses focus and then instantly gains it again and I am not able to select a row (row selection - enabled, cell editing - disabled, Selection Mode - Single Selection) 当我单击表格以选择一行时,文本字段失去焦点,然后立即又获得焦点,因此我无法选择一行(行选择-启用,单元格编辑-禁用,选择模式-单选)

Code for When TextField Gains and Loses Focus & when typing: TextField获得和失去焦点时以及键入时的代码:

private void productBrandTextFieldFocusGained(java.awt.event.FocusEvent evt) {                                                  
    brandSearchPanel.setVisible(true);
    displayBrands();
} 

private void productBrandTextFieldFocusLost(java.awt.event.FocusEvent evt) {                                                
    brandSearchPanel.setVisible(false);
}

private void productBrandTextFieldKeyReleased(java.awt.event.KeyEvent evt) {                                                  
    searchBrand();                           
}   

Images showing what happens when Focus is Gained, Lost or Typing @ http://sdrv.ms/14TEq2T 这些图像显示了获得,丢失或键入焦点时发生的情况, 网址为http://sdrv.ms/14TEq2T

Also, I would like to be able to go through the rows using the DOWN Key. 另外,我希望能够使用DOWN键浏览各行。 Please suggest some code that when Down key is pressed the focus is lost from textfield and row is selected and pressing UP OR DOWN key will go up or down the rows. 请建议一些代码,当按下Down键时,焦点将从文本字段中丢失,并且已选择行,而按UP或DOWN键将使行向上或向下移动。

The FocusEvent has a method getOppositeComponent() that you might be able to use in your logic. FocusEvent有一个方法getOppositeComponent() ,您可能会在逻辑中使用它。

When the text field loses focus you invoke this method. 当文本字段失去焦点时,您将调用此方法。 If the component gaining focus is the table, then you don't make the table panel invisible. 如果要获得焦点的组件是表格,则不要使表格面板不可见。 Then when the user clicks on the table focus should stay there. 然后,当用户单击表时,焦点应停留在该位置。

To handle the Up/Down arrow keys from the text field you would need to use Key Bindings . 要处理文本字段中的上/下箭头键,您需要使用Key Bindings You would create an Action that gets the current selection from the table and then changes the selection by +/- 1 depending on the key that was pressed. 您将创建一个动作,该动作从表中获取当前选择,然后根据所按下的键将选择更改+/- 1。 Read the Swing tutorial on How to Use Key Bindings for more information. 阅读有关如何使用键绑定的Swing教程获取更多信息。

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

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