This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to know which component has the current focus  RSS feed

 
J Roxas
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, everybody!

I have a frame which contains 2 jpanels (masterPanel and detailPanel). In masterPanel i have 5 jtextfields (jtxtField1...jtxtField5) and in detailPanel i also have 5 jtextfields (jtxtField6...jtxtField10). I have 2 questions regarding this scenario.

1. How will i know which among the jtextfields has the focus currently? To
which component will i attach the proper listener?

2. Is it possible to know which among my 2 panels does the currently
focused jtextfield belong? How?

Thanks in advance!
 
KArol Muszynski
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

1) use Component.getFocusOwner() (JFrame.getFocusOwner())
* Returns the child Component of this Window that has focus if this Window
* is focused; returns null otherwise.

As result you will have your focused JTextField. And then you can
2) use JTextField.getParent()

Best Regards,
KArol Muszynski
 
J Roxas
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply, KArol!
 
J Roxas
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply, KArol!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!