qflib 0.98.1

de.qfs.lib.gui
Class SwingUtil

java.lang.Object
  |
  +--de.qfs.lib.gui.SwingUtil

public abstract class SwingUtil
extends java.lang.Object

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

Version:
$Revision: 1.28 $
Author:
Gregor Schmid

Inner Class Summary
static class SwingUtil.ConstrainedViewport
          This class extends JViewport to restrict the arguments passed to setViewPosition to nonnegative coordinates.
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 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 void collapseAll(javax.swing.JTree tree, javax.swing.tree.TreePath path)
          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 expandAll(javax.swing.JTree tree, javax.swing.tree.TreePath path)
          Expand a tree node and all its child nodes recursively.
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.

ALL_EVENTS

public static final int ALL_EVENTS
Identifier for all events.

PAINT_EVENTS

public static final int PAINT_EVENTS
Identifier for paint events.

INPUT_EVENTS

public static final int INPUT_EVENTS
Identifier for input events.

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.
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.

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.
Parameters:
c - The root of the hierarchy to break up.

expandAll

public static void expandAll(javax.swing.JTree tree,
                             javax.swing.tree.TreePath path)
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.

collapseAll

public static void collapseAll(javax.swing.JTree tree,
                               javax.swing.tree.TreePath path)
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.

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

qflib 0.98.1