qflib 0.98.1

de.qfs.lib.gui
Class FocusFixer

java.lang.Object
  |
  +--javax.swing.FocusManager
        |
        +--javax.swing.DefaultFocusManager
              |
              +--de.qfs.lib.gui.FocusFixer
All Implemented Interfaces:
java.util.EventListener, java.awt.event.WindowListener

public class FocusFixer
extends javax.swing.DefaultFocusManager
implements java.awt.event.WindowListener

This class fixes an annoying bug that only occurs in JDK 1.1 with the motif Toolkit. In that constellation, whenever a window loses the focus it forgets which component had the focus last. When the window regains the focus, no component is focused and navigation with the tab key restarts at the first component.

To fix this problem, FocusFixer extends DefaultFocusManager to keep track of focused components and implements WindowListener in order to return the focus to the right component when a window is opened or activated.

To fix focus management for any Window, simply get a FocusFixer instance with instance and use its addWindow method. Don't forget to remove the window with removeWindow when you are getting rid of it.

You can use FocusFixer regardless of what JDK you use and what operating system resp. what AWT Toolkit you are running on. If it is not the JDK 1.1 motif combination, FocusFixer will silently ignore all calls.

Since:
0.97.0
Version:
$Revision: 1.7 $
Author:
Gregor Schmid

Fields inherited from class javax.swing.FocusManager
FOCUS_MANAGER_CLASS_PROPERTY
 
Method Summary
 void addWindow(java.awt.Window window)
          Register a window with a FocusFixer so it will keep track of the focus owner and restore the focus whenever the window is openened or activated.
 void focusNextComponent(java.awt.Component com)
          Focus the next component.
 void focusPreviousComponent(java.awt.Component com)
          Focus the previous component.
static FocusFixer instance()
          Get the FocusFixer instance for the ThreadGroup of the calling Thread.
 void processKeyEvent(java.awt.Component com, java.awt.event.KeyEvent e)
          Process one key event.
 void removeWindow(java.awt.Window window)
          Remove a window from a FocusFixer so it will no longer handle its focus.
 void windowActivated(java.awt.event.WindowEvent e)
          Restore the focus to the recent focus owner.
 void windowClosed(java.awt.event.WindowEvent e)
          Does nothing.
 void windowClosing(java.awt.event.WindowEvent e)
          Does nothing.
 void windowDeactivated(java.awt.event.WindowEvent e)
          Does nothing.
 void windowDeiconified(java.awt.event.WindowEvent e)
          Does nothing.
 void windowIconified(java.awt.event.WindowEvent e)
          Does nothing.
 void windowOpened(java.awt.event.WindowEvent e)
          Restore the focus to the recent focus owner.
 
Methods inherited from class javax.swing.DefaultFocusManager
compareTabOrder, getComponentAfter, getComponentBefore, getFirstComponent, getLastComponent
 
Methods inherited from class javax.swing.FocusManager
disableSwingFocusManager, getCurrentManager, isFocusManagerEnabled, setCurrentManager
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

instance

public static FocusFixer instance()
Get the FocusFixer instance for the ThreadGroup of the calling Thread. If no FocusFixer for that ThreadGroup has been created yet, create a new one and register it as the FocusManager for that ThreadGroup.
Returns:
The FocusFixer instance.
See Also:
FocusManager.setCurrentManager(javax.swing.FocusManager)

addWindow

public void addWindow(java.awt.Window window)
Register a window with a FocusFixer so it will keep track of the focus owner and restore the focus whenever the window is openened or activated. You should call removeWindow when the window is disposed in order to clean up all references to it.
Parameters:
window - The window to register.

removeWindow

public void removeWindow(java.awt.Window window)
Remove a window from a FocusFixer so it will no longer handle its focus.
Parameters:
window - The window to remove.

windowActivated

public void windowActivated(java.awt.event.WindowEvent e)
Restore the focus to the recent focus owner.
Specified by:
windowActivated in interface java.awt.event.WindowListener
Parameters:
e - The event containing the details.

windowOpened

public void windowOpened(java.awt.event.WindowEvent e)
Restore the focus to the recent focus owner.
Specified by:
windowOpened in interface java.awt.event.WindowListener
Parameters:
e - The event containing the details.

windowDeactivated

public void windowDeactivated(java.awt.event.WindowEvent e)
Does nothing.
Specified by:
windowDeactivated in interface java.awt.event.WindowListener
Parameters:
e - The event containing the details.

windowClosed

public void windowClosed(java.awt.event.WindowEvent e)
Does nothing.
Specified by:
windowClosed in interface java.awt.event.WindowListener
Parameters:
e - The event containing the details.

windowClosing

public void windowClosing(java.awt.event.WindowEvent e)
Does nothing.
Specified by:
windowClosing in interface java.awt.event.WindowListener
Parameters:
e - The event containing the details.

windowIconified

public void windowIconified(java.awt.event.WindowEvent e)
Does nothing.
Specified by:
windowIconified in interface java.awt.event.WindowListener
Parameters:
e - The event containing the details.

windowDeiconified

public void windowDeiconified(java.awt.event.WindowEvent e)
Does nothing.
Specified by:
windowDeiconified in interface java.awt.event.WindowListener
Parameters:
e - The event containing the details.

processKeyEvent

public void processKeyEvent(java.awt.Component com,
                            java.awt.event.KeyEvent e)
Process one key event. If the component is inside a Window that has been registered with the FocusFixer, remember it as the current focus owner.
Overrides:
processKeyEvent in class javax.swing.DefaultFocusManager
Parameters:
com - The component on which the key event occured.
e - The event.

focusNextComponent

public void focusNextComponent(java.awt.Component com)
Focus the next component. If the component is inside a Window that has been registered with the FocusFixer, remember the new focus owner.
Overrides:
focusNextComponent in class javax.swing.DefaultFocusManager
Parameters:
com - Focus the component that is next in the focus order from this one.

focusPreviousComponent

public void focusPreviousComponent(java.awt.Component com)
Focus the previous component. If the component is inside a Window that has been registered with the FocusFixer, remember the new focus owner.
Overrides:
focusPreviousComponent in class javax.swing.DefaultFocusManager
Parameters:
com - Focus the component that is the previous one from this one in the focus order.

qflib 0.98.1