Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using transferFocus() in JPanel 1

Status
Not open for further replies.

rdoor

Technical User
May 4, 2002
43
US
Ihave a problem which I cannot figure out. I have a math drill program which I've made and I made one JPanel class (named MainPanel extends JPanel) which contains 30 JPanels (named ProblemPanel extends JPanel), each containing one math drill problem. Each of the drill JP's has a JTextField which is used for entering the answer. The whole shebang is contained in a JFrame class (named MadMinute extends JFrame) for display.

When the kids press enter, the entry in the JTextField is evaluated for correctness and the focus is supposed to jump to the JPanel with the next problem. I've used an anonymous inner class with addActionListener() method of the JTextField and included the transferFocus() in the inner class. How does transferFocus work? I can tab between the ProblemPanels, but I can't make the Enter key work. I'm using Java 1.3.1.

Roy from Iowa
 
I built a sample app as you described and had no problem getting it to work. So if you don't mind a few questions...

When enter is pressed, does the focus stay where it is, or does the cursor "disappear" and the focus doesn't seem to be anywhere until you click another text field?

Are you setting the next focusable component manually?

What layout managers are you using on your panels?

Are you doing something to set the focus initially that may be getting in the way later?

Are you starting other threads in the inner classes?

Are you calling transferFocus on the JTextField components or on the JPanel components? (should be the JTextField)

Thanks...
 
Dear

Thanks! Many thanks! Calling the transferFocus method of the JTextField rather than the JPanel was successful. I've never been quite clear on how transferFocus worked. How does Java decide what the next component for focus should be when transferFocus() is called? I was ready to try to set the nextFocusableComponent manually, but I'd never done this before and I wasn't looking forward to it.

Roy
 
I think it has to do with the order things are added to the frame.

Glad I could help!

Ian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top