org.tbee.swing
Class SwingUtilities

java.lang.Object
  extended by org.tbee.swing.SwingUtilities

public class SwingUtilities
extends java.lang.Object

Some swing utilites waitForThreadToFinish has been moved to the FoxtrotUtils

Version:
$Revision: 1.100 $
Author:
$Author: toeukpap $

Field Summary
static java.awt.Insets EMPTY_INSETS
           
static java.lang.String SOURCECODE_VERSION
          Standard variable for determining version of a class file.
 
Constructor Summary
SwingUtilities()
           
 
Method Summary
static void centerOnScreen(java.awt.Window jwindow)
          Center a frame to the screen
static java.awt.Component cloneComponent(java.awt.Component component)
          Create a clone of a component
static javax.swing.border.Border createCompoundedBorder(javax.swing.border.Border... borders)
          Merge borders using CompoundBorders; first border is outside, last border is inside
static java.awt.image.BufferedImage createDesktopImage()
          Convenience method to create a BufferedImage of the desktop
static java.awt.image.BufferedImage createImage(java.awt.Component component)
          Create a BufferedImage for AWT components.
static java.awt.image.BufferedImage createImage(javax.swing.JComponent component)
           
static java.awt.image.BufferedImage createImage(javax.swing.JComponent component, java.awt.Rectangle region)
           
static java.awt.image.BufferedImage createImage(java.awt.Rectangle region)
          Create a BufferedImage from a rectangular region on the screen.
static java.awt.image.BufferedImage createImageFromComponent(java.awt.Component comp)
          Create an image of a component, by having the component paint onto the image
static javax.swing.JDialog createJDialog(java.awt.Component owner)
          create a JDialog
static javax.swing.JDialog createJDialog(java.awt.Component owner, boolean modal)
          create a JDialog
static javax.swing.JDialog createJDialog(java.awt.Component owner, boolean modal, java.awt.Container container)
          create a JDialog
static javax.swing.JDialog createJDialog(java.awt.Component owner, java.awt.Container container)
          create a JDialog
static javax.swing.JDialog createJDialog(java.awt.Component owner, java.awt.Dialog.ModalityType modal)
          create a JDialog
static javax.swing.JDialog createJDialog(java.awt.Component owner, java.lang.String title)
          create a JDialog
static javax.swing.JDialog createJDialog(java.awt.Component owner, java.lang.String title, boolean modal)
          create a JDialog
static javax.swing.JDialog createJDialog(java.awt.Component owner, java.lang.String title, boolean modal, java.awt.Container container)
          create a JDialog
static javax.swing.JDialog createJDialog(java.awt.Component owner, java.lang.String title, java.awt.Container container)
          create a JDialog
static javax.swing.JDialog createJDialog(java.awt.Component owner, java.lang.String title, java.awt.Dialog.ModalityType modality)
           
static javax.swing.JDialog createJDialog(java.awt.Component owner, java.lang.String title, java.awt.Dialog.ModalityType modality, boolean maximize)
          create a JDialog
static javax.swing.JFrame createJFrame()
           
static javax.swing.JFrame createJFrame(java.awt.Component... component)
           
static javax.swing.JFrame createJFrame(java.awt.Component component, int width, int height)
           
static javax.swing.JFrame createJFrame(java.awt.Container container)
           
static javax.swing.JFrame createJFrame(java.awt.Container container, int width, int height)
           
static javax.swing.JFrame createJFrame(java.lang.String name, java.awt.Component... component)
           
static javax.swing.JFrame createJFrame(java.lang.String name, java.awt.Component component, int width, int height)
           
static javax.swing.JFrame createJFrame(java.lang.String name, java.awt.Container container)
           
static javax.swing.JFrame createJFrame(java.lang.String name, java.awt.Container container, int width, int height)
           
static javax.swing.JScrollPane createTransparentJScrollpane()
           
static javax.swing.JScrollPane createTransparentJScrollpane(java.awt.Component c)
           
static java.lang.String describe(java.awt.Component c)
           
static java.lang.Thread detachFromAwt(java.lang.Runnable runnable)
          Detach from AWT thread; if already detached, just run.
static void disableAllComponents(java.awt.Container container)
          Disable all components in a container (recusively)
static java.awt.event.ActionListener exceptionsAsJOptionPane(java.awt.event.ActionListener actionListener)
          Show IllegalArgumentExceptions and IllegalStateExceptions in a JOptionPane and then discard
static java.awt.event.ActionListener exceptionsAsStatusBarAndJOptionPane(StatusBar statusBar, java.awt.event.ActionListener actionListener)
          Show IllegalArgumentExceptions and IllegalStateExceptions in a JOptionPane and then discard
static java.awt.Container findContainer(java.awt.Component c, java.lang.Class containerClass)
          Find the toplevel container, whether that is a dialog or frame
static java.awt.Container findToplevelContainer(java.awt.Component c)
          Find the toplevel container, whether that is a dialog or frame
static void fitInScreen(java.awt.Window window)
           
static void fix16DialogMemoryLeak()
          In java 1.6 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6497929
static javax.swing.JDialog fixDecoratedJDialogProblem(javax.swing.JDialog dialog)
          This is something that should be hooked in to the "new JFrame" with AOP?
static javax.swing.JFrame fixDecoratedJFrameProblem(javax.swing.JFrame frame)
          This is something that should be hooked in to the "new JFrame" with AOP?
static java.util.List<java.awt.Component> flattenComponentTree(javax.swing.JComponent c)
          Flatten the whole component tree into a list, so it is easily possible to search if some component is present
static java.awt.Component forceRefresh(java.awt.Component c)
          Code to force a component to refresh its contents
static java.awt.Component forceRelayout(java.awt.Component c)
          Code to force a component to relayout
static int getBorderSizeBottom(javax.swing.JComponent c)
           
static int getBorderSizeLeft(javax.swing.JComponent c)
           
static int getBorderSizeRight(javax.swing.JComponent c)
           
static int getBorderSizeTop(javax.swing.JComponent c)
          return the border size
static java.awt.Dimension getScreenSize(java.awt.Window window)
          Gets the maximum working screen size.
static boolean hasFocusElseRequestAndReclick(javax.swing.JButton button)
          When a keyboard shortcut is used to trigger a button, the focus is not moved.
static void invokeAndWait(java.lang.Runnable runnable)
          Run a runnable as part of the EDT; if already on the EDT just run
try {
SwingUtilities.invokeAndWait( new Runnable() { public void run() {
...
} } );
}
catch (InvocationTargetException e) {e.printStackTrace(); }
catch (InterruptedException e) {e.printStackTrace(); }
static void invokeAndWait2(java.lang.Runnable runnable)
          Run a runnable as part of the EDT; if already on the EDT just run This method also catches the exceptions and dumps them to stderr.

SwingUtilities.invokeAndWait2( new Runnable() { public void run() {
...
} } );
static void invokeLater(int milliseconds, java.lang.Runnable runnable)
          Execute in swing thread after a delay
static void invokeLater(java.lang.Runnable runnable)
          Run later in the EDT; if already on the EDT just run
static boolean isComponentVisibleToUser(java.awt.Component c)
          See if this container is still visible to the user (any component in the path toward a window or applet must be visible).
static void jumpTo(javax.swing.JComponent component, javax.swing.JScrollPane jscrollpane)
           
static void jumpToComponentInScrollpane(javax.swing.JComponent component)
           
static void makeWindowAtLeastTheWidthOfComponent(java.awt.Window window, java.awt.Component component)
          Make a window at least the size of the component (combined with placeAtComponent you can make popups)
static void paintImmediately(javax.swing.JComponent c)
          Force a repaint within the EDT (normally a repaint places a new event on the EDT queue).
static void placeAtComponent(java.awt.Window window, java.awt.Component component)
          Place a window at the same position as a component
static void placeBelowComponent(java.awt.Window window, java.awt.Component component)
          Place a window at the same position as a component
static boolean pointIsInComponent(java.awt.Component c, java.awt.Point p)
          Check to see if a point falls within the component's boundary
static boolean print(java.awt.Component component)
          print a component TODO: the print now printers in the length over multiple pages, we also need a way to print in the width
static void quickKeybind(javax.swing.JComponent c, javax.swing.KeyStroke k, javax.swing.Action a)
          Quickly bind a keystroke to an action
static void registerEnterAsFocusTravelsalKey()
          As you've noticed, when you press Enter on fields like JTextField et al, an ActionEvent is generated, and focus is not transferred to the next field in the focus cycle.
static void setAntiAlias(boolean value)
          Tell swing to anti alias text
static void setDefaultFont(java.lang.String fontname)
          Change the default font on all swing components
static void setDefaultFont(java.lang.String fontname, double scale)
          Change the default font on all swing components
static void setEnableAll(java.awt.Container container, java.util.List ignoredComponents, boolean enable)
          enable all components in a container (recusively)
static void setHorizontalAlignment(javax.swing.JSpinner spinner, int aligment)
          Set the alignment of a spinner
static void setup()
          Some generic setup (must be called immediatly in the main)
static void setupLAF()
          Setup the JVM to do full LAF
static javax.swing.ProgressMonitor showProgressMonitorImmediately(javax.swing.ProgressMonitor progressMonitor)
           
static void synchronizeTwoScrollbars(javax.swing.JScrollBar sb1, javax.swing.JScrollBar sb2)
          Two scrollbars scrolling simultaniously.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SOURCECODE_VERSION

public static final java.lang.String SOURCECODE_VERSION
Standard variable for determining version of a class file.

See Also:
Constant Field Values

EMPTY_INSETS

public static final java.awt.Insets EMPTY_INSETS
Constructor Detail

SwingUtilities

public SwingUtilities()
Method Detail

setup

public static void setup()
Some generic setup (must be called immediatly in the main)


fix16DialogMemoryLeak

public static void fix16DialogMemoryLeak()
In java 1.6 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6497929


setupLAF

public static void setupLAF()
Setup the JVM to do full LAF


setAntiAlias

public static void setAntiAlias(boolean value)
Tell swing to anti alias text

Parameters:
fontname -

setDefaultFont

public static void setDefaultFont(java.lang.String fontname)
Change the default font on all swing components

Parameters:
fontname -

setDefaultFont

public static void setDefaultFont(java.lang.String fontname,
                                  double scale)
Change the default font on all swing components

Parameters:
fontname -
scale -

invokeLater

public static void invokeLater(java.lang.Runnable runnable)
Run later in the EDT; if already on the EDT just run

Parameters:
runnable -

invokeLater

public static void invokeLater(int milliseconds,
                               java.lang.Runnable runnable)
Execute in swing thread after a delay

Parameters:
milliseconds -
runnable -

invokeAndWait

public static void invokeAndWait(java.lang.Runnable runnable)
                          throws java.lang.reflect.InvocationTargetException,
                                 java.lang.InterruptedException
Run a runnable as part of the EDT; if already on the EDT just run
try {
SwingUtilities.invokeAndWait( new Runnable() { public void run() {
...
} } );
}
catch (InvocationTargetException e) {e.printStackTrace(); }
catch (InterruptedException e) {e.printStackTrace(); }

Throws:
java.lang.reflect.InvocationTargetException
java.lang.InterruptedException

invokeAndWait2

public static void invokeAndWait2(java.lang.Runnable runnable)
Run a runnable as part of the EDT; if already on the EDT just run This method also catches the exceptions and dumps them to stderr.

SwingUtilities.invokeAndWait2( new Runnable() { public void run() {
...
} } );


detachFromAwt

public static java.lang.Thread detachFromAwt(java.lang.Runnable runnable)
Detach from AWT thread; if already detached, just run.
SwingUtilities.detachFromAwt( new Runnable() { public void run() {
...
} } );

Returns:
the thread that is processing the runnable, this can be the current thread!

getScreenSize

public static java.awt.Dimension getScreenSize(java.awt.Window window)
Gets the maximum working screen size. This function differs from the Toolkit.getScreenSize() by allowing for the space that any permanent fixtures that native application windows do not have access to in a given OS. As an example, the space for the toolbar in Windows - native applications do not get that pixel area for their use.

Parameters:
window - the window that will use the screen
Returns:
the size of the available screen in pixels

fitInScreen

public static void fitInScreen(java.awt.Window window)
Parameters:
window -

findToplevelContainer

public static java.awt.Container findToplevelContainer(java.awt.Component c)
Find the toplevel container, whether that is a dialog or frame

Parameters:
c -
Returns:

findContainer

public static java.awt.Container findContainer(java.awt.Component c,
                                               java.lang.Class containerClass)
Find the toplevel container, whether that is a dialog or frame

Parameters:
c -
Returns:

isComponentVisibleToUser

public static boolean isComponentVisibleToUser(java.awt.Component c)
See if this container is still visible to the user (any component in the path toward a window or applet must be visible).

Parameters:
c -
Returns:

flattenComponentTree

public static java.util.List<java.awt.Component> flattenComponentTree(javax.swing.JComponent c)
Flatten the whole component tree into a list, so it is easily possible to search if some component is present

Parameters:
c -
Returns:

forceRelayout

public static java.awt.Component forceRelayout(java.awt.Component c)
Code to force a component to relayout


forceRefresh

public static java.awt.Component forceRefresh(java.awt.Component c)
Code to force a component to refresh its contents


centerOnScreen

public static void centerOnScreen(java.awt.Window jwindow)
Center a frame to the screen

Parameters:
jwindow -

placeAtComponent

public static void placeAtComponent(java.awt.Window window,
                                    java.awt.Component component)
Place a window at the same position as a component

Parameters:
window -
component -

placeBelowComponent

public static void placeBelowComponent(java.awt.Window window,
                                       java.awt.Component component)
Place a window at the same position as a component

Parameters:
window -
component -

makeWindowAtLeastTheWidthOfComponent

public static void makeWindowAtLeastTheWidthOfComponent(java.awt.Window window,
                                                        java.awt.Component component)
Make a window at least the size of the component (combined with placeAtComponent you can make popups)

Parameters:
window -
component -

fixDecoratedJFrameProblem

public static javax.swing.JFrame fixDecoratedJFrameProblem(javax.swing.JFrame frame)
This is something that should be hooked in to the "new JFrame" with AOP?


createJFrame

public static javax.swing.JFrame createJFrame()

createJFrame

public static javax.swing.JFrame createJFrame(java.awt.Container container)

createJFrame

public static javax.swing.JFrame createJFrame(java.lang.String name,
                                              java.awt.Container container)

createJFrame

public static javax.swing.JFrame createJFrame(java.lang.String name,
                                              java.awt.Container container,
                                              int width,
                                              int height)

createJFrame

public static javax.swing.JFrame createJFrame(java.awt.Container container,
                                              int width,
                                              int height)

createJFrame

public static javax.swing.JFrame createJFrame(java.lang.String name,
                                              java.awt.Component... component)

createJFrame

public static javax.swing.JFrame createJFrame(java.awt.Component... component)

createJFrame

public static javax.swing.JFrame createJFrame(java.lang.String name,
                                              java.awt.Component component,
                                              int width,
                                              int height)

createJFrame

public static javax.swing.JFrame createJFrame(java.awt.Component component,
                                              int width,
                                              int height)

fixDecoratedJDialogProblem

public static javax.swing.JDialog fixDecoratedJDialogProblem(javax.swing.JDialog dialog)
This is something that should be hooked in to the "new JFrame" with AOP?


createJDialog

public static javax.swing.JDialog createJDialog(java.awt.Component owner)
create a JDialog

Parameters:
owner - the component of which the container will be the owner of this dialog

createJDialog

public static javax.swing.JDialog createJDialog(java.awt.Component owner,
                                                java.awt.Container container)
create a JDialog

Parameters:
owner - the component of which the container will be the owner of this dialog
container - the content of the dialog

createJDialog

public static javax.swing.JDialog createJDialog(java.awt.Component owner,
                                                boolean modal)
create a JDialog

Parameters:
owner - the component of which the container will be the owner of this dialog
modal - is the dialog modal

createJDialog

public static javax.swing.JDialog createJDialog(java.awt.Component owner,
                                                java.awt.Dialog.ModalityType modal)
create a JDialog

Parameters:
owner - the component of which the container will be the owner of this dialog
modal - is the dialog modal

createJDialog

public static javax.swing.JDialog createJDialog(java.awt.Component owner,
                                                boolean modal,
                                                java.awt.Container container)
create a JDialog

Parameters:
owner - the component of which the container will be the owner of this dialog
modal - is the dialog modal
container - the content of the dialog

createJDialog

public static javax.swing.JDialog createJDialog(java.awt.Component owner,
                                                java.lang.String title)
create a JDialog

Parameters:
owner - the component of which the container will be the owner of this dialog
title - dialog title

createJDialog

public static javax.swing.JDialog createJDialog(java.awt.Component owner,
                                                java.lang.String title,
                                                java.awt.Container container)
create a JDialog

Parameters:
owner - the component of which the container will be the owner of this dialog
title - dialog title
container - the content of the dialog

createJDialog

public static javax.swing.JDialog createJDialog(java.awt.Component owner,
                                                java.lang.String title,
                                                boolean modal)
create a JDialog

Parameters:
owner - the component of which the container will be the owner of this dialog
title - dialog title
modal - is the dialog modal

createJDialog

public static javax.swing.JDialog createJDialog(java.awt.Component owner,
                                                java.lang.String title,
                                                boolean modal,
                                                java.awt.Container container)
create a JDialog

Parameters:
owner - the component of which the container will be the owner of this dialog
title - dialog title
modal - is the dialog modal
container - the content of the dialog

createJDialog

public static javax.swing.JDialog createJDialog(java.awt.Component owner,
                                                java.lang.String title,
                                                java.awt.Dialog.ModalityType modality,
                                                boolean maximize)
create a JDialog

Parameters:
owner - the component of which the container will be the owner of this dialog

createJDialog

public static javax.swing.JDialog createJDialog(java.awt.Component owner,
                                                java.lang.String title,
                                                java.awt.Dialog.ModalityType modality)

getBorderSizeTop

public static int getBorderSizeTop(javax.swing.JComponent c)
return the border size


getBorderSizeLeft

public static int getBorderSizeLeft(javax.swing.JComponent c)

getBorderSizeRight

public static int getBorderSizeRight(javax.swing.JComponent c)

getBorderSizeBottom

public static int getBorderSizeBottom(javax.swing.JComponent c)

createCompoundedBorder

public static javax.swing.border.Border createCompoundedBorder(javax.swing.border.Border... borders)
Merge borders using CompoundBorders; first border is outside, last border is inside

Parameters:
borders -
Returns:
border

synchronizeTwoScrollbars

public static void synchronizeTwoScrollbars(javax.swing.JScrollBar sb1,
                                            javax.swing.JScrollBar sb2)
Two scrollbars scrolling simultaniously. The range must be indentical


createTransparentJScrollpane

public static javax.swing.JScrollPane createTransparentJScrollpane()

createTransparentJScrollpane

public static javax.swing.JScrollPane createTransparentJScrollpane(java.awt.Component c)

jumpToComponentInScrollpane

public static void jumpToComponentInScrollpane(javax.swing.JComponent component)
Parameters:
component -

jumpTo

public static void jumpTo(javax.swing.JComponent component,
                          javax.swing.JScrollPane jscrollpane)
Parameters:
component -
jscrollpane -

describe

public static java.lang.String describe(java.awt.Component c)

createImageFromComponent

public static java.awt.image.BufferedImage createImageFromComponent(java.awt.Component comp)
Create an image of a component, by having the component paint onto the image

Parameters:
comp -

createImage

public static java.awt.image.BufferedImage createImage(javax.swing.JComponent component)

createImage

public static java.awt.image.BufferedImage createImage(javax.swing.JComponent component,
                                                       java.awt.Rectangle region)

createImage

public static java.awt.image.BufferedImage createImage(java.awt.Component component)
                                                throws java.awt.AWTException
Create a BufferedImage for AWT components.

Parameters:
component - AWT component to create image from
fileName - name of file to be created or null
Returns:
image the image for the given region
Throws:
java.io.IOException - if an error occurs during writing
java.awt.AWTException

createImage

public static java.awt.image.BufferedImage createImage(java.awt.Rectangle region)
                                                throws java.awt.AWTException
Create a BufferedImage from a rectangular region on the screen.

Parameters:
region - region on the screen to create image from
Returns:
image the image for the given region
Throws:
java.awt.AWTException - see Robot class constructors
java.io.IOException - if an error occurs during writing

createDesktopImage

public static java.awt.image.BufferedImage createDesktopImage()
                                                       throws java.awt.AWTException
Convenience method to create a BufferedImage of the desktop

Returns:
image the image for the given region
Throws:
java.awt.AWTException - see Robot class constructors
java.io.IOException - if an error occurs during writing

hasFocusElseRequestAndReclick

public static boolean hasFocusElseRequestAndReclick(javax.swing.JButton button)
When a keyboard shortcut is used to trigger a button, the focus is not moved. This means that any pending edits might no be committed. In order to make sure any edits are committed, focus must be force-moved. This means using "requestFocus", however this does not actually move the focus, but places an event on the AWT thread. That means the focus will be moved after the current event is done. So we must post-pone the current event and let the focus event be processed first. This method does this by: - placing a requestFocus event on the queue - then placing a click event on the queue In this way the focus will be moved and then when the click is reprocessed. When using this method, the event should be exited if this method returns false. Otherwise processing may continue.


cloneComponent

public static java.awt.Component cloneComponent(java.awt.Component component)
Create a clone of a component

Parameters:
component -
Returns:

registerEnterAsFocusTravelsalKey

public static void registerEnterAsFocusTravelsalKey()
As you've noticed, when you press Enter on fields like JTextField et al, an ActionEvent is generated, and focus is not transferred to the next field in the focus cycle. But what if you are not interested in ActionEvent's on fields, and would rather Enter have the same effect as Tab, maybe because your users are used to having to enter data with the Enter key? But then we aint gonna get those ActionEvent's no more, which may or may not be a problem. http://weblogs.java.net/blog/evanx/archive/2007/03/tipsy_snipsy_fo_1.html


paintImmediately

public static void paintImmediately(javax.swing.JComponent c)
Force a repaint within the EDT (normally a repaint places a new event on the EDT queue).


disableAllComponents

public static void disableAllComponents(java.awt.Container container)
Disable all components in a container (recusively)


setEnableAll

public static void setEnableAll(java.awt.Container container,
                                java.util.List ignoredComponents,
                                boolean enable)
enable all components in a container (recusively)


print

public static boolean print(java.awt.Component component)
print a component TODO: the print now printers in the length over multiple pages, we also need a way to print in the width


exceptionsAsJOptionPane

public static java.awt.event.ActionListener exceptionsAsJOptionPane(java.awt.event.ActionListener actionListener)
Show IllegalArgumentExceptions and IllegalStateExceptions in a JOptionPane and then discard


exceptionsAsStatusBarAndJOptionPane

public static java.awt.event.ActionListener exceptionsAsStatusBarAndJOptionPane(StatusBar statusBar,
                                                                                java.awt.event.ActionListener actionListener)
Show IllegalArgumentExceptions and IllegalStateExceptions in a JOptionPane and then discard


quickKeybind

public static void quickKeybind(javax.swing.JComponent c,
                                javax.swing.KeyStroke k,
                                javax.swing.Action a)
Quickly bind a keystroke to an action

Parameters:
c - Component
k - Keystroke
a - Action

pointIsInComponent

public static boolean pointIsInComponent(java.awt.Component c,
                                         java.awt.Point p)
Check to see if a point falls within the component's boundary

Parameters:
c -
p - must be in screen coordinates
Returns:

setHorizontalAlignment

public static void setHorizontalAlignment(javax.swing.JSpinner spinner,
                                          int aligment)
Set the alignment of a spinner

Parameters:
spinner -
aligment - SwingConstants

showProgressMonitorImmediately

public static javax.swing.ProgressMonitor showProgressMonitorImmediately(javax.swing.ProgressMonitor progressMonitor)
Parameters:
progressMonitor -


Copyright © 2010. All Rights Reserved.