qflib 0.99

de.qfs.lib.gui
Class SwingUtil

java.lang.Object
  extended byde.qfs.lib.gui.SwingUtil

public abstract class SwingUtil
extends java.lang.Object

A collection of utilities and Swing problem workarounds that go beyond SwingUtilities.

Author:
Gregor Schmid

Nested Class Summary
static class SwingUtil.ConstrainedViewport
          This class extends JViewport to restrict the arguments passed to setViewPosition to nonnegative coordinates.
static class SwingUtil.FocusDisabler
          Visitor that disables the focus for JDK 1.4 JScrollBars.
static interface SwingUtil.UnsafeRunnable
          This interface is similar to Runnable, except that the run method may return a value and throw any kind of Exception.
 
Field Summary
static int ALL_EVENTS
          Identifier for all events.
static int INPUT_EVENTS
          Identifier for input events.
static int NO_EVENTS
          Identifier for no events.
static int OTHER_EVENTS
          Identifier for the rest of the events.
static int PAINT_EVENTS
          Identifier for paint events.
 
Constructor Summary
SwingUtil()
           
 
Method Summary
static void autoSizeTableColumn(javax.swing.JTable table, int column)
          Resize a table column so its fits exactly the broadest of its visible cells.
static void centerWindow(java.awt.Window win)
          Place a window in the center of the screen.
static void centerWindowOnComponent(java.awt.Window win, java.awt.Component com, boolean constrain)
          Place a window in the center of a component.
static void cleanup(java.awt.Component c)
          Break up a Component hierarchy to increase chances for garbage collection.
static void closeOnEscape(java.awt.Window win)
          Add a KeyboardListener to a Window that posts a WINDOW_CLOSING event on it when Escape is pressed.
static boolean collapseAll(javax.swing.JTree tree, javax.swing.tree.TreePath path, int limit)
          Collapse a tree node and all its child nodes recursively.
static void constrainScroll(javax.swing.JScrollPane scrollPane)
          Constrain the scrolling range of a JScrollPane so that it never scrolls to a negative view position.
static void deactivateInputContexts(java.awt.Window win)
          There's a problem: The AWT keeps around all the owned dialogs and disposes these when the frame is disposed.
static void disableScrollBarFocus(java.awt.Component com)
          Traverse a component hierarchy and make all JScrollBars non-focusable.
static boolean expandAll(javax.swing.JTree tree, javax.swing.tree.TreePath path, int limit)
          Expand a tree node and all its child nodes recursively.
static void focusLater(java.awt.Component com)
          A delayed version of the requestFocus method, a workaround for some weird JDK 1.4.1/Motif focus behavior that messes up focus requests after closing a modal dialog.
static boolean invokeAndWait(java.lang.Runnable runnable)
          Invoke a thread inside the swing EventLoop and wait for it to finish.
static void prepareKeymaps()
          Prepare Keymaps of JTextField and JPasswordField, so that the return key invokes a possible default button.
static void scrollDownLine(javax.swing.JScrollPane scrollPane)
          Scroll the contents of a scrollPane down by one line if possible.
static void scrollUpLine(javax.swing.JScrollPane scrollPane)
          Scroll the contents of a scrollPane up by one line if possible.
static void setDefaultEnterBinding(javax.swing.text.JTextComponent text)
          Restore the default binding for the return key, if it was changed by prepareKeymaps.
static void showPopup(javax.swing.JPopupMenu menu, java.awt.Component parent, int x, int y, boolean constrain)
          Bring up a JPopupMenu but make sure that it is fully visible on the screen (if at all possible).
static java.lang.Object withEvents(SwingUtil.UnsafeRunnable runnable, boolean noInput)
          Deprecated. Please use withEvents instead.
static java.lang.Object withEvents(SwingUtil.UnsafeRunnable runnable, int delay, int discard)
          Execute some code in a background thread, while continuing to handle AWT events from the current tread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_EVENTS

public static final int NO_EVENTS
Identifier for no events.

See Also:
Constant Field Values

ALL_EVENTS

public static final int ALL_EVENTS
Identifier for all events.

See Also:
Constant Field Values

PAINT_EVENTS

public static final int PAINT_EVENTS
Identifier for paint events.

See Also:
Constant Field Values

INPUT_EVENTS

public static final int INPUT_EVENTS
Identifier for input events.

See Also:
Constant Field Values

OTHER_EVENTS

public static final int OTHER_EVENTS
Identifier for the rest of the events. Must include all unused types for compatibility with possible future extensions.

See Also:
Constant Field Values
Constructor Detail

SwingUtil

public SwingUtil()
Method Detail

invokeAndWait

public static boolean invokeAndWait(java.lang.Runnable runnable)
Invoke a thread inside the swing EventLoop and wait for it to finish.

Parameters:
runnable - The Runnable to invoke.
Returns:
True if everything was OK, false, if the current Thread was interrupted while waititing for the EventLoop.

withEvents

public static java.lang.Object withEvents(SwingUtil.UnsafeRunnable runnable,
                                          boolean noInput)
                                   throws java.lang.reflect.InvocationTargetException
Deprecated. Please use withEvents instead.

Execute some code in a background Thread, while continuing to handle AWT Events from the current Thread. InputEvents like mouse or keyboard input can optionally be discarded (useful together with a wait cursor). This works only if called from an AWT EventLoop Thread. When called from some other Thread, the Runnable simply gets executed directly. In this case there is no way to block input events.

Parameters:
runnable - The UnsafeRunnable to execute.
noInput - If true, InputEvents will be discarded.
Returns:
The result from runnable's execution.
Throws:
java.lang.RuntimeException - If runnable's run method throws a RuntimeException, it is ismply passed on
java.lang.reflect.InvocationTargetException - If runnable's run method* throws an Exception.

withEvents

public static java.lang.Object withEvents(SwingUtil.UnsafeRunnable runnable,
                                          int delay,
                                          int discard)
                                   throws java.lang.reflect.InvocationTargetException
Execute some code in a background thread, while continuing to handle AWT events from the current tread.

This method distinguishes between three kinds of events identified by

PAINT_EVENTS
All kinds of paint events.
INPUT_EVENTS
All kind of input events, e.g. keyboard or mouse events.
OTHER_EVENTS
All the rest.
For each of these kinds of events, there are three possible ways to handle them:
dispatch
Dispatch the event immediately as it arrives. This is the default for all events.
delay
Delay the event until the background thread is finished. Useful if handling the event could interfere with the background thread.
discard
Discard the event as if it never happended. Makes sense for input events if a wait cursor is being displayed while the background thread is active.
This works only if called from an AWT EventLoop Thread. When called from some other Thread, the Runnable simply gets executed directly. In this case there is no way to block or delay events.

This method is more or less reentrant, meaning that recusive calls don't fail horribly, but they can lead to confusion since the last call will determine the delayed and discarded event types for all calls.

Parameters:
runnable - The UnsafeRunnable to execute.
delay - Binary OR of event types that should be delayed.
discard - Binary OR of event types that should be discarded.
Returns:
The result from runnable's execution.
Throws:
java.lang.RuntimeException - If runnable's run method throws a RuntimeException, it is ismply passed on
java.lang.reflect.InvocationTargetException - If runnable's run method* throws an Exception.

prepareKeymaps

public static void prepareKeymaps()
Prepare Keymaps of JTextField and JPasswordField, so that the return key invokes a possible default button. This can be undone for individual TextComponents with setDefaultEnterBinding.


setDefaultEnterBinding

public static void setDefaultEnterBinding(javax.swing.text.JTextComponent text)
Restore the default binding for the return key, if it was changed by prepareKeymaps.

Parameters:
text - The TextComponent for which to restore the binding.

closeOnEscape

public static void closeOnEscape(java.awt.Window win)
Add a KeyboardListener to a Window that posts a WINDOW_CLOSING event on it when Escape is pressed. Useful for modal dialogs.

Parameters:
win -

constrainScroll

public static void constrainScroll(javax.swing.JScrollPane scrollPane)
Constrain the scrolling range of a JScrollPane so that it never scrolls to a negative view position. This helps fix a bug in the Swing 1.1.1 action for PageDown in a JTree that places the tree at the bottom of the JScrollPane if its size is less than the visible area.

This method replaces the JScrollPane's JViewport with one that checks the argument to setViewPosition.

Parameters:
scrollPane - The JScrollPane to adapt.

showPopup

public static void showPopup(javax.swing.JPopupMenu menu,
                             java.awt.Component parent,
                             int x,
                             int y,
                             boolean constrain)
Bring up a JPopupMenu but make sure that it is fully visible on the screen (if at all possible). Optionally it can be constrained to its parent Window as well.

Parameters:
menu - The JPopupMenu to show.
parent - The parent Component of the menu.
x - The targeted x coordinate.
y - The targeted y coordinate.
constrain - Whether the menu should be constrained to its parent window.

scrollUpLine

public static void scrollUpLine(javax.swing.JScrollPane scrollPane)
Scroll the contents of a scrollPane up by one line if possible. Note that this action typically corresponds to pressing the DOWN arrow key.

Parameters:
scrollPane - The scroll pane to scroll.

scrollDownLine

public static void scrollDownLine(javax.swing.JScrollPane scrollPane)
Scroll the contents of a scrollPane down by one line if possible. Note that this action typically corresponds to pressing the UP arrow key.

Parameters:
scrollPane - The scroll pane to scroll.

disableScrollBarFocus

public static void disableScrollBarFocus(java.awt.Component com)
Traverse a component hierarchy and make all JScrollBars non-focusable. Ignored unless run with JDK 1.4 and above.

Parameters:
com - The root of the hierarchy.
Since:
0.99.0

focusLater

public static void focusLater(java.awt.Component com)
A delayed version of the requestFocus method, a workaround for some weird JDK 1.4.1/Motif focus behavior that messes up focus requests after closing a modal dialog.

Parameters:
com - The component to request the focus for.
Since:
0.99.0

cleanup

public static void cleanup(java.awt.Component c)
Break up a Component hierarchy to increase chances for garbage collection. Swing has quite a few static caches that can delay or even prevent garbage collection. This method tries to remove as many references as possible. Also implements a workaround fow a problem with some JDKs that leaves active InputContexts around that prevent a window from being disposed.

Parameters:
c - The root of the hierarchy to break up.

deactivateInputContexts

public static void deactivateInputContexts(java.awt.Window win)
There's a problem: The AWT keeps around all the owned dialogs and disposes these when the frame is disposed. For reasons unknown some may still have an active InputContext which can cause an IllegalStateException, so we have to "deactivate" these contexts. For JDK 1.1 this is a NOOP.

Parameters:
win - The window whose InputContexts to clear.
Since:
0.99.0

expandAll

public static boolean expandAll(javax.swing.JTree tree,
                                javax.swing.tree.TreePath path,
                                int limit)
Expand a tree node and all its child nodes recursively.

Parameters:
tree - The tree whose nodes to expand.
path - Path to the node to start at.
limit - Maximum number of nodes to expand.
Returns:
True if everything was fine, false if the limit was exceeded and the operation aborted.

collapseAll

public static boolean collapseAll(javax.swing.JTree tree,
                                  javax.swing.tree.TreePath path,
                                  int limit)
Collapse a tree node and all its child nodes recursively.

Parameters:
tree - The tree whose nodes to collapse.
path - Path to the node to start at.
limit - Maximum number of nodes to expand.
Returns:
True if everything was fine, false if the limit was exceeded and the operation aborted.

autoSizeTableColumn

public static void autoSizeTableColumn(javax.swing.JTable table,
                                       int column)
Resize a table column so its fits exactly the broadest of its visible cells. This will set the columns width as well as its preferred width. Only works if the table's parent is a JViewport, i.e. the table is nested in a JScrollPane.

Parameters:
table - The table that contains the column.
column - The colunm index in table coordinates.

centerWindow

public static void centerWindow(java.awt.Window win)
Place a window in the center of the screen.

Parameters:
win - The window.
Since:
0.96.0

centerWindowOnComponent

public static void centerWindowOnComponent(java.awt.Window win,
                                           java.awt.Component com,
                                           boolean constrain)
Place a window in the center of a component.

Parameters:
win - The window.
com - The component to center on.
constrain - Whether to constrain the window to the visible screen.
Since:
0.99.0

qflib 0.99