This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I have a jtextfield, every time F7 is pressed on this jtextfield i'm displaying a jlist. The user selects from that jlist by pressing ENTER key. To trap this event, I added a key listener to the jlist and once enter key is pressed, i'm processing the selected item and returning focus to the original jtextfield where F7 was pressed. What happening is my cursor is navigated automatically to the next jtextfield and not on the original jtextfield. What I think is that this is the result of my enter key on the jlist not being consumed and being translated as the enter key on the original jtextfield. Can anyone pls. try to analyze this scenario and advice me what to do as I am new to Java. Also pls. advice me if how im handling the enter key in jlist is correct or not.
> i'm processing the selected item and returning focus to the original > jtextfield where F7 was pressed
at the end of the JList processing are you calling textField.requestFocusInWindow();
if you are, post a sample program of your code: just a frame with 2 textfields, F7 code to display JList, and the code where the JList closes and focus returns to the next textfield.
> advice me if how im handling the enter key in jlist is correct or not.
impossible to say without seeing the code. Normally you would use a ListSelectionListener, but if using this you have to trap the selection from the de-selection, using if(lse.getValueIsAdjusting() == false)//has been selected
Joined: Aug 04, 2005
Thanks for the reply, Michael.
While preparing for the code you were asking me to post, I was able to realize what went wrong with it. My requestFocusInWindow was inside a nested IF and as I traced down, it was also calling a procedure which is making another requestFocusInWindow into another jTextField that was causing the program to focus into another jTextField, it was solely my mistake, sorry for this... Now the program is working well!