org.pf.swing
Class FocusEnforcer

java.lang.Object
  extended by java.lang.Thread
      extended by org.pf.swing.FocusEnforcer
All Implemented Interfaces:
java.lang.Runnable

public class FocusEnforcer
extends java.lang.Thread

This subclass of Thread can be used to set the focus of a window to a specific component. The thread is running until the component has the focus. This is particularly useful with modal dialogs (i.e. JDialog) where it is not possible in another way to set the focus to a component other than the first in the dialog.

Version:
1.0
Author:
M.Duchrow

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
FocusEnforcer(javax.swing.JComponent focusComponent)
          Initialize the new instance the component to put the focus on.
FocusEnforcer(javax.swing.JComponent focusComponent, java.lang.String name)
          Initialize the new instance the component to put the focus on.
 
Method Summary
static void enforceFocusOn(javax.swing.JComponent focusComponent)
          Starts a thread to put the focus on the given component.
 void run()
          Actually execute the thread's purpose and set the focus on the desired component.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FocusEnforcer

public FocusEnforcer(javax.swing.JComponent focusComponent)
Initialize the new instance the component to put the focus on.

Parameters:
focusComponent - The component that should get the focus.

FocusEnforcer

public FocusEnforcer(javax.swing.JComponent focusComponent,
                     java.lang.String name)
Initialize the new instance the component to put the focus on.

Parameters:
focusComponent - The component that should get the focus.
name - The name of the thread
Method Detail

enforceFocusOn

public static void enforceFocusOn(javax.swing.JComponent focusComponent)
Starts a thread to put the focus on the given component. The thread runs until the given component has gained the focus.

Parameters:
focusComponent - The component that should get the focus.

run

public void run()
Actually execute the thread's purpose and set the focus on the desired component. If it isn't possible for any reason then this method gives up after 5 seconds.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread