org.tbee.swing
Class SwingUtilities

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

public class SwingUtilities
extends Object

Some swing utilities waitForThreadToFinish has been moved to the FoxtrotUtils

Version:
$Revision: 1.112 $
Author:
$Author: toeukpap $

Field Summary
static Insets EMPTY_INSETS
           
static String SOURCECODE_VERSION
          Standard variable for determining version of a class file.
 
Constructor Summary
SwingUtilities()
           
 
Method Summary
static void addEnterAsClick(JButton button)
           
static void addEnterAsFocusTravelsalKey()
          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 addEnterAsFocusTravelsalKey(Component component)
          Same but per component
static void centerOnScreen(Window jwindow)
          Center a frame to the screen
static Component cloneComponent(Component component)
          Create a clone of a component
static Border createCompoundedBorder(Border... borders)
          Merge borders using CompoundBorders; first border is outside, last border is inside
static BufferedImage createDesktopImage()
          Convenience method to create a BufferedImage of the desktop
static BufferedImage createImage(Component component)
          Create a BufferedImage for AWT components.
static BufferedImage createImage(JComponent component)
           
static BufferedImage createImage(JComponent component, Rectangle region)
           
static BufferedImage createImage(Rectangle region)
          Create a BufferedImage from a rectangular region on the screen.
static BufferedImage createImageFromComponent(Component comp)
          Create an image of a component, by having the component paint onto the image
static JDialog createJDialog(Component owner)
          create a JDialog
static JDialog createJDialog(Component owner, boolean modal)
          create a JDialog
static JDialog createJDialog(Component owner, boolean modal, Container container)
          create a JDialog
static JDialog createJDialog(Component owner, Container container)
          create a JDialog
static JDialog createJDialog(Component owner, Dialog.ModalityType modal)
          create a JDialog
static JDialog createJDialog(Component owner, String title)
          create a JDialog
static JDialog createJDialog(Component owner, String title, boolean modal)
          create a JDialog
static JDialog createJDialog(Component owner, String title, boolean modal, Container container)
          create a JDialog
static JDialog createJDialog(Component owner, String title, Container container)
          create a JDialog
static JDialog createJDialog(Component owner, String title, Dialog.ModalityType modality)
           
static JDialog createJDialog(Component owner, String title, Dialog.ModalityType modality, boolean maximize)
          create a JDialog
static JDialog createJDialog(Component owner, String title, Dialog.ModalityType modal, Container container)
          create a JDialog
static JFrame createJFrame()
           
static JFrame createJFrame(Component... component)
           
static JFrame createJFrame(Component component, int width, int height)
           
static JFrame createJFrame(Container container)
           
static JFrame createJFrame(Container container, int width, int height)
           
static JFrame createJFrame(String name, Component... component)
           
static JFrame createJFrame(String name, Component component, int width, int height)
           
static JFrame createJFrame(String name, Container container)
           
static JFrame createJFrame(String name, Container container, int width, int height)
           
static JScrollPane createTransparentJScrollpane()
           
static JScrollPane createTransparentJScrollpane(Component c)
           
static String describe(Component c)
           
static Thread detachFromAwt(Runnable runnable)
          Detach from AWT thread; if already detached, just run.
static Thread detachFromAwtShowExceptions(Component c, Runnable runnable)
           
static void disableAllComponents(Container container)
          Disable all components in a container (recusively)
static ActionListener exceptionsAsJOptionPane(ActionListener actionListener)
          Show IllegalArgumentExceptions and IllegalStateExceptions in a JOptionPane and then discard
static Runnable exceptionsAsJOptionPane(Component component, Runnable runnable)
          Show IllegalArgumentExceptions and IllegalStateExceptions in a JOptionPane and then discard
static Runnable exceptionsAsStatusBarAndJOptionPane(Component component, StatusBar statusBar, Runnable runnable)
          Show IllegalArgumentExceptions and IllegalStateExceptions in a JOptionPane and then discard
static ActionListener exceptionsAsStatusBarAndJOptionPane(StatusBar statusBar, ActionListener actionListener)
          Show IllegalArgumentExceptions and IllegalStateExceptions in a JOptionPane and then discard
static JComponent findByName(JComponent container, String name)
          Find a JComponent by its name
static Container findContainer(Component c, Class containerClass)
          Find the toplevel container, whether that is a dialog or frame
static Container findToplevelContainer(Component c)
          Find the toplevel container, whether that is a dialog or frame
static void fitInScreen(Window window)
           
static void fix16DialogMemoryLeak()
          In java 1.6 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6497929
static JDialog fixDecoratedJDialogProblem(JDialog dialog)
          This is something that should be hooked in to the "new JFrame" with AOP?
static JFrame fixDecoratedJFrameProblem(JFrame frame)
          This is something that should be hooked in to the "new JFrame" with AOP?
static List<Component> flattenComponentTree(JComponent c)
          Flatten the whole component tree into a list, so it is easily possible to search if some component is present
static Component forceRefresh(Component c)
          Code to force a component to refresh its contents
static Component forceRelayout(Component c)
          Code to force a component to relayout
static int getBorderSizeBottom(JComponent c)
           
static int getBorderSizeLeft(JComponent c)
           
static int getBorderSizeRight(JComponent c)
           
static int getBorderSizeTop(JComponent c)
          return the border size
static Dimension getScreenSize(Window window)
          Gets the maximum working screen size.
static boolean hasFocusElseRequestAndReclick(JButton button)
          When a keyboard shortcut is used to trigger a button, the focus is not moved.
static void invokeAndWait(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
<E> E
invokeAndWait2(Callable<E> callable)
          InvokeAndWait with return value
static void invokeAndWait2(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 invokeAndWait2ShowExceptions(Component c, Runnable runnable)
           
static void invokeLater(int milliseconds, Runnable runnable)
          Execute in swing thread after a delay
static void invokeLater(Runnable runnable)
          Run later in the EDT; if already on the EDT just run
static void invokeLaterShowExceptions(Component c, Runnable runnable)
           
static boolean isComponentVisibleToUser(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(JComponent component, JScrollPane jscrollpane)
           
static void jumpToComponentInScrollpane(JComponent component)
           
static void makeWindowAtLeastTheWidthOfComponent(Window window, Component component)
          Make a window at least the size of the component (combined with placeAtComponent you can make popups)
static void paintImmediately(JComponent c)
          Force a repaint within the EDT (normally a repaint places a new event on the EDT queue).
static void placeAtComponent(Window window, Component component)
          Place a window at the same position as a component
static void placeBelowComponent(Window window, Component component)
          Place a window at the same position as a component
static boolean pointIsInComponent(Component c, Point p)
          Check to see if a point falls within the component's boundary
static boolean print(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(JComponent c, KeyStroke k, Action a)
          Quickly bind a keystroke to an action
static void setAntiAlias(boolean value)
          Tell swing to anti alias text
static void setDefaultFont(String fontname)
          Change the default font on all swing components
static void setDefaultFont(String fontname, double scale)
          Change the default font on all swing components
static void setEnableAll(Container container, boolean enable)
           
static void setEnableAll(Container container, List ignoredComponents, boolean enable)
          enable all components in a container (recusively)
static void setHorizontalAlignment(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 Runnable showExceptions(Component c, Runnable runnable)
          Any exception throw in the runnable's run() will be shown in an error optionpane
static ProgressMonitor showProgressMonitorImmediately(ProgressMonitor progressMonitor)
           
static void synchronizeTwoScrollbars(JScrollBar sb1, 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 String SOURCECODE_VERSION
Standard variable for determining version of a class file.

See Also:
Constant Field Values

EMPTY_INSETS

public static final 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(String fontname)
Change the default font on all swing components

Parameters:
fontname -

setDefaultFont

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

Parameters:
fontname -
scale -

invokeLater

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

Parameters:
runnable -

invokeLaterShowExceptions

public static void invokeLaterShowExceptions(Component c,
                                             Runnable runnable)

invokeLater

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

Parameters:
milliseconds -
runnable -

invokeAndWait

public static void invokeAndWait(Runnable runnable)
                          throws InvocationTargetException,
                                 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:
InvocationTargetException
InterruptedException

invokeAndWait2

public static void invokeAndWait2(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() {
...
} } );


invokeAndWait2ShowExceptions

public static void invokeAndWait2ShowExceptions(Component c,
                                                Runnable runnable)

invokeAndWait2

public static <E> E invokeAndWait2(Callable<E> callable)
InvokeAndWait with return value

Parameters:
callable -
Returns:

detachFromAwt

public static Thread detachFromAwt(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, but also another thread which is already running when returning from this call

detachFromAwtShowExceptions

public static Thread detachFromAwtShowExceptions(Component c,
                                                 Runnable runnable)

showExceptions

public static Runnable showExceptions(Component c,
                                      Runnable runnable)
Any exception throw in the runnable's run() will be shown in an error optionpane

Parameters:
c -
runnable -
Returns:

getScreenSize

public static Dimension getScreenSize(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(Window window)
Parameters:
window -

findToplevelContainer

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

Parameters:
c -
Returns:

findContainer

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

Parameters:
c -
Returns:

isComponentVisibleToUser

public static boolean isComponentVisibleToUser(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 List<Component> flattenComponentTree(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 Component forceRelayout(Component c)
Code to force a component to relayout


forceRefresh

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


centerOnScreen

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

Parameters:
jwindow -

placeAtComponent

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

Parameters:
window -
component -

placeBelowComponent

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

Parameters:
window -
component -

makeWindowAtLeastTheWidthOfComponent

public static void makeWindowAtLeastTheWidthOfComponent(Window window,
                                                        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 JFrame fixDecoratedJFrameProblem(JFrame frame)
This is something that should be hooked in to the "new JFrame" with AOP?


createJFrame

public static JFrame createJFrame()

createJFrame

public static JFrame createJFrame(Container container)

createJFrame

public static JFrame createJFrame(String name,
                                  Container container)

createJFrame

public static JFrame createJFrame(String name,
                                  Container container,
                                  int width,
                                  int height)

createJFrame

public static JFrame createJFrame(Container container,
                                  int width,
                                  int height)

createJFrame

public static JFrame createJFrame(String name,
                                  Component... component)

createJFrame

public static JFrame createJFrame(Component... component)

createJFrame

public static JFrame createJFrame(String name,
                                  Component component,
                                  int width,
                                  int height)

createJFrame

public static JFrame createJFrame(Component component,
                                  int width,
                                  int height)

fixDecoratedJDialogProblem

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


createJDialog

public static JDialog createJDialog(Component owner)
create a JDialog

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

createJDialog

public static JDialog createJDialog(Component owner,
                                    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 JDialog createJDialog(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 JDialog createJDialog(Component owner,
                                    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 JDialog createJDialog(Component owner,
                                    boolean modal,
                                    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 JDialog createJDialog(Component owner,
                                    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 JDialog createJDialog(Component owner,
                                    String title,
                                    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 JDialog createJDialog(Component owner,
                                    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 JDialog createJDialog(Component owner,
                                    String title,
                                    boolean modal,
                                    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 JDialog createJDialog(Component owner,
                                    String title,
                                    Dialog.ModalityType modal,
                                    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 JDialog createJDialog(Component owner,
                                    String title,
                                    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 JDialog createJDialog(Component owner,
                                    String title,
                                    Dialog.ModalityType modality)

getBorderSizeTop

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


getBorderSizeLeft

public static int getBorderSizeLeft(JComponent c)

getBorderSizeRight

public static int getBorderSizeRight(JComponent c)

getBorderSizeBottom

public static int getBorderSizeBottom(JComponent c)

createCompoundedBorder

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

Parameters:
borders -
Returns:
border

synchronizeTwoScrollbars

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


createTransparentJScrollpane

public static JScrollPane createTransparentJScrollpane()

createTransparentJScrollpane

public static JScrollPane createTransparentJScrollpane(Component c)

jumpToComponentInScrollpane

public static void jumpToComponentInScrollpane(JComponent component)
Parameters:
component -

jumpTo

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

describe

public static String describe(Component c)

createImageFromComponent

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

Parameters:
comp -

createImage

public static BufferedImage createImage(JComponent component)

createImage

public static BufferedImage createImage(JComponent component,
                                        Rectangle region)

createImage

public static BufferedImage createImage(Component component)
                                 throws 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:
IOException - if an error occurs during writing
AWTException

createImage

public static BufferedImage createImage(Rectangle region)
                                 throws 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:
AWTException - see Robot class constructors
IOException - if an error occurs during writing

createDesktopImage

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

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

hasFocusElseRequestAndReclick

public static boolean hasFocusElseRequestAndReclick(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 Component cloneComponent(Component component)
Create a clone of a component

Parameters:
component -
Returns:

addEnterAsFocusTravelsalKey

public static void addEnterAsFocusTravelsalKey()
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


addEnterAsFocusTravelsalKey

public static void addEnterAsFocusTravelsalKey(Component component)
Same but per component

Parameters:
component -

addEnterAsClick

public static void addEnterAsClick(JButton button)

paintImmediately

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


disableAllComponents

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


setEnableAll

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


setEnableAll

public static void setEnableAll(Container container,
                                boolean enable)

print

public static boolean print(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 ActionListener exceptionsAsJOptionPane(ActionListener actionListener)
Show IllegalArgumentExceptions and IllegalStateExceptions in a JOptionPane and then discard


exceptionsAsStatusBarAndJOptionPane

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


exceptionsAsJOptionPane

public static Runnable exceptionsAsJOptionPane(Component component,
                                               Runnable runnable)
Show IllegalArgumentExceptions and IllegalStateExceptions in a JOptionPane and then discard


exceptionsAsStatusBarAndJOptionPane

public static Runnable exceptionsAsStatusBarAndJOptionPane(Component component,
                                                           StatusBar statusBar,
                                                           Runnable runnable)
Show IllegalArgumentExceptions and IllegalStateExceptions in a JOptionPane and then discard


quickKeybind

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

Parameters:
c - Component
k - Keystroke
a - Action

pointIsInComponent

public static boolean pointIsInComponent(Component c,
                                         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(JSpinner spinner,
                                          int aligment)
Set the alignment of a spinner

Parameters:
spinner -
aligment - SwingConstants

showProgressMonitorImmediately

public static ProgressMonitor showProgressMonitorImmediately(ProgressMonitor progressMonitor)
Parameters:
progressMonitor -

findByName

public static JComponent findByName(JComponent container,
                                    String name)
Find a JComponent by its name

Parameters:
container -
name -
Returns:


Copyright © 2011 KnowledgePlaza. All Rights Reserved.