Skip to main content

Focus lost on a Panel

1 reply [Last post]
Anonymous

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Scott Violet

>[post from hsantos on javadesktop.org]
>
> Hi
> I have 2 JPanels, each of them containing some JTextFields, JComboBoxes
> where user inputs data.
> I'd like to perform some validation when user leaves panel1 and enters
> panel2, but I don't know how to catch that event.
> Adding a FocusListener to each panel doesn't seem to work ...

Adding a FocusListener to each panel doesn't work as the panels never
get focus (at least by default they won't), only the child widgets get
focus. You'll have to be careful with JComboBoxs too. Editable
JComboBoxs are implemented as multiple components, so that the
JComboBox never really gets focus. You can dig out the text field
(editor), which is the widget that get focus.

On Fri, Sep 05, 2003 at 06:46:50AM -0400, swing-feedback@javadesktop.org wrote:
> [posted by mgrev on javadeskop.org]
>
> Adding the fucus listener to the JTextFields/Combos is one approach
> that works for sure.
>
> As a more flexible solution you can subclass the JPanels and intercept
> (override) jPanel.add(Component c) and add the listener to 'c' there.
>
> You can also subclass the JTextFields and JComboBoxes to make them
> validate thier content everytime the focus is lost, but that is may not
> be what you want, depending on your context.

Other options are to use an InputVerifier, which can be attached to
each of the widgets (if you need to show an option pane as the result
of a bogus value be careful of bug 4532517, there is a work around in
the bug report), you could also attached a PropertyChangeListener to
the KeyboardFocusManager to get notification of ALL focus changes.

Again, be careful with JComboBox as if you go the InputVerifier route
you'll need to attach the InputVerifier to the editor, not the
JComboBox.

-Scott