0

I have lots of JFrames with several components. They're generally built dynamically with all components added dynamically as well, but some are built manually.

Want I want: I want to add a new component, say a read-only JTextField, which prints information about the currently focused component in the JFrame. The purpose is to have a really easy way to see where each component fetches its data from (they're generally bound to fields from a database or to some method just returning the data they need) and other interesting stuff. Let's just call this the "DebugInfoComponent".

My current idea, which I feel is somewhat primitive: I could "just" add a FocusListener to every component of every JFrame, which then notifies the DebugInfoComponent, which then calls setText with the relevant information based on the component being focused.

My question: Is there a better way?

slaursen
  • 83
  • 1
  • 3
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Feb 06 '15 at 09:36
  • 1
    Take a look at [`KeyboardFocusManager`](http://docs.oracle.com/javase/7/docs/api/java/awt/KeyboardFocusManager.html) – MadProgrammer Feb 06 '15 at 09:46
  • @MadProgrammer I just made a quick test, and it seems to be exactly what I was looking for - thank you! The key is to add a property listener on focusOwner and that seems to be it :-) (post as an answer and I'll accept it) – slaursen Feb 07 '15 at 13:24

1 Answers1

0

Use the KeyboardFocusManger, you can use it's property change support to monitor changes to the focus state of the application.

You probably want to monitor the focusOwner property, but you should investigate the other properties as well

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366