aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to set focus on submit button on press of enter key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to set focus on submit button on press of enter key" Watch "How to set focus on submit button on press of enter key" New topic
Author

How to set focus on submit button on press of enter key

Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
Hello,

I have two problem in swing JTable

1) On press of enter in the JTextfield focus should go to the first cell of the JTable. (It is going there but two consecutive press of enter key)
2) On press of enter key in the last cell of the table it should go to the next component that is Submit button.


currently i am using this code to move my focus to the next component. But in case of table it is not working.

Please help me with link or example where i can get solution for this.

Thanks
Rajesh
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

Rajesh k Jha wrote:1) On press of enter in the JTextfield focus should go to the first cell of the JTable. (It is going there but two consecutive press of enter key)

Use an ActionListener. ActionListeners are triggered by pressing enter on JTextFields.

2) On press of enter key in the last cell of the table it should go to the next component that is Submit button.

Check out how key bindings work. If you press enter (identified through key bindings) and the last cell is currently selected (query the JTable) then perform the action.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
Hello Rob, I think i was unable to make you understand my problem , my problem is
I want to put focus on the first cell of the JTable cell (0,0) when i press enter at the textfield and again when i fill data in all the cell and i am in the last cell then on press of enter it should come out of Table and focus should go to the JButton.

I think this should give more clarity about my existing problem.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

I understood just fine.
Rajesh k Jha wrote:I want to put focus on the first cell of the JTable cell (0,0) when i press enter at the textfield

Like I said, use an ActionListener on the text field to select the first cell, then request focus on the JTable.

and again when i fill data in all the cell and i am in the last cell then on press of enter it should come out of Table and focus should go to the JButton.

And there you use a key binding on the JTable; for the enter keystroke, if the last cell is selected request focus on the JButton.
Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
Hello Rob,

Theoratically it sounds good, but i have been trying the same for the last few days, So please if you could provide some example or link where i can get practical clue.
As i am not very expert and unbale to find how table behaves when entered is pressed and how table identifies the first and last row to get the focus on the first row and move out when last cell is pressed entered button.

Thanks
Rajesh
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Rajesh k Jha wrote:Theoratically it sounds good, but i have been trying the same for the last few days, So please if you could provide some example or link where i can get practical clue.

Check out the Oracle Swing tutorials section on key binding to see how this works as well as to see sample code. Then give it a try. If your code still doesn't work, create and post an SSCCE and we'll be more than happy to work with you on this.

Much luck!
Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
Hello Stein,

Much to my dismay, i couldn't solve my problem yet, hence posting you code for review and help.
I am using this code for the movement in the horizontal way on press of "Enter" key and coming out of table on press of "Tab" button.


I am using following code to move from one JTextFieldto another JTextField


now the problem is when i am in the last JTextfield and press "Enter" Button then it doesn't go to the first cell of JTable but on press of Enter key twice it goes there, so I want this functionality on single press of Enter key.

And again when the focus is on the last cell then on press of enter key it should go to the next component(Submit Button)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to set focus on submit button on press of enter key
 
Similar Threads
JTable and tab order.
problem in colors of cells JTable
JTable Editing
tabbing in JTable
tabbing in JTable