-
Enhancement
-
Resolution: Duplicate
-
P4
-
None
-
1.2.0
-
generic
-
generic
Name: vi73552 Date: 04/01/99
This is a request for an enhancement. One problem we all have when developing
GUI applications is that it is very difficult if not impossible for a component
to determine at any moment which component has the focus. It is particularly
difficult when trying to do error checking on a group of components. For
example, suppose a programmer has implemented a phone number input component.
It consists of a JPanel that contains seven "digit input" components. The
programmer wants to know when the focus has left the JPanel so that he can
verify the phone number. Because the JPanel does not get focus itself, he tries
to set up each digit to be a focus listener, but it doesn't help. He learns when
a digit has lost focus, but did another digit gain it, or was it a component
outside of the JPanel? He can't determine THAT question unless he knows who
got the focus.
I propose a simple solution that I think would solve this problem. First,
enhance the FocusManager class so that it knows who has the focus at all times.
This involves adding a currentFocusComponent field with accompanying get-set
methods. Second, modify Component (or JComponent) so that it calls the focus
manager's "setCurrentFocusComponent(this) method as soon as it gains focus.
On the surface this does not sound like it would help. When a digit component
loses focus, it might call focus manager's getCurrentFocusComponent method, but
all it would get is a reference to itself because the focusGained event has not
yet been delivered. What I propose in the preceding paragraph is a bare minimum
that I would need. I would add one more thing.
I suggest that you also add PropertyChangeSupport to FocusManager. Whenever a
component registers itself as the new focus owner, FocusManager does a
firePropertyChange("focusGained",lostFocusComponent, gainedFocusComponent) to
all property change listeners.
This solves the JPanel's problem. He registers himself as a
property change listener with FocusManager. He now knows when his children gain
focus and when they lose it, and it can now do the error checking at the
proper time.
But wait, there's more. I can then derive a new FocusManager that solves other
problems. When my focus manager gets a new component registration, it can check
the component's type and take some action. If the component losing focus is a
JTable, it can stop cell editing. If the loser is a TextComponent, it can stop
the text component's editing and do error checking. This is a one-stop location
for closing these open editors, and it doesn't require adding focus listeners
to every individual JTable and JTextComponent.
I think this is a useful tool to add to the programmer's toolkit because it
solves some awkward problems. Moreover, I believe it is simple to implement and
should have virtually no affect on performance. I apologize for wasting your
time if I've missed the boat here.
(Review ID: 56385)
======================================================================
- duplicates
-
JDK-4290675 Focus Management Enhancements
- Closed