I have JFrame which is having multiple Panels on it. Each Panel is having some Components designed on that. I want to Change the Background Color of Component(JTextField) when it gain Focus.
I have Many TextFields and I dont want to Write FocusListener for all the Components.
Is there any solution to do it in a Smart Manner.
I have JFrame which is having multiple Panels on it. Each Panel is having
Share
You should definitely consider your design, as suggested by @Robin. Creating and configuring all components of an application through a factory helps to make it robust against requirement changes as there is a single location to change instead of being scattered all over the code.
Moreover, an individual listener per component keeps the control near-by to where the focus induced property changes occur, thus not needing state handling in a global listener.
That said, the technical (as in: use with care!) solution for a global focusListener is to register a propertyChangeListener with the KeyboardFocusManager.
A quick code snippet (with very crude state handling 🙂