edu.rice.cs.plt.swing
Class SwingUtil

java.lang.Object
  extended by edu.rice.cs.plt.swing.SwingUtil

public class SwingUtil
extends Object


Constructor Summary
SwingUtil()
           
 
Method Summary
static void add(Container parent, Component... children)
          Add the given components to parent in sequence.
static ActionListener asActionListener(Runnable r)
          Convert a Runnable to an ActionListener
static ActionListener asActionListener(Runnable1<? super ActionEvent> r)
          Convert a Runnable1 to an ActionListener
static Runnable1<ActionEvent> asRunnable(ActionListener l)
          Convert an ActionListener to a Runnable1
static FileFilter asSwingFileFilter(FileFilter filter, String description)
          Define a Swing file filter (for use with JFileChoosers) in terms if a FileFilter.
static FileFilter asSwingFileFilter(FilePredicate p, String description)
          Define a Swing file filter (for use with JFileChoosers) in terms if a FileFilter.
static FileFilter asSwingFileFilter(Predicate<? super File> p, String description)
          Define a Swing file filter (for use with JFileChoosers) in terms if a FileFilter.
static void attemptClearEventQueue()
          Call @link{#clearEventQueue}, but ignore any resulting InterruptedException.
static void clearEventQueue()
          Wait for all items in the event queue to be handled.
static void displayWindow(Window w)
          Shortcut for the boilerplate code to pack and display a window (such as a JFrame).
static ActionListener disposeAction(Window w)
          Create an action to invoke Window.dispose() on the given window
static Action getAction(JTextComponent component, String actionName)
          Search the given text component's list of actions for an action with the given name.
static Map<String,Action> getActions(JTextComponent component)
          Create a map view of component.getActions(), where the keys are the action names.
static String getClipboardSelection(Component c)
           
static Color gray(float degree)
          Create a shade of gray with the given degree of darkness.
static void invokeAndWait(Runnable task)
          Runs the the task in the event thread, blocking until it is complete.
static void invokeLater(Runnable task)
          Runs the task synchronously if the current thread is the event thread; otherwise passes it to the event thread to be run asynchronously after all events already on the queue have been processed.
static JPanel makeBorderPanel()
          Make a JPanel with a BorderLayout.
static JPanel makeBorderPanel(int margin)
          Make a JPanel with a BorderLayout; set the border to the given margin on all sides.
static JPanel makeBorderPanel(int vMargin, int hMargin)
          Make a JPanel with a BorderLayout; set the border to the given vertical/horizontal margins.
static JPanel makeBorderPanel(int topMargin, int leftMargin, int bottomMargin, int rightMargin)
          Make a JPanel with a BorderLayout; set the border to the given margins.
static JFrame makeDisposableFrame(String title, int width, int height)
          Boilerplate for creating a JFrame.
static JPanel makeFlowPanel()
          Make a JPanel with a FlowLayout.
static JPanel makeFlowPanel(int margin)
          Make a JPanel with a FlowLayout; set the border to the given margin on all sides.
static JPanel makeFlowPanel(int vMargin, int hMargin)
          Make a JPanel with a FlowLayout; set the border to the given vertical/horizontal margins.
static JPanel makeFlowPanel(int topMargin, int leftMargin, int bottomMargin, int rightMargin)
          Make a JPanel with a FlowLayout; set the border to the given margins.
static JPanel makeHorizontalBoxPanel()
          Make a JPanel with a horizontal BoxLayout.
static JPanel makeHorizontalBoxPanel(int margin)
          Make a JPanel with a horizontal BoxLayout; set the border to the given margin on all sides.
static JPanel makeHorizontalBoxPanel(int vMargin, int hMargin)
          Make a JPanel with a horizontal BoxLayout; set the border to the given vertical/horizontal margins.
static JPanel makeHorizontalBoxPanel(int topMargin, int leftMargin, int bottomMargin, int rightMargin)
          Make a JPanel with a horizontal BoxLayout; set the border to the given margins.
static JFrame makeMainApplicationFrame(String title, int width, int height)
          Boilerplate for creating a JFrame to be used as the main application window.
static JFrame makeReusableFrame(String title, int width, int height)
          Boilerplate for creating a JFrame.
static JPanel makeVerticalBoxPanel()
          Make a JPanel with a vertical BoxLayout.
static JPanel makeVerticalBoxPanel(int margin)
          Make a JPanel with a vertical BoxLayout; set the border to the given margin on all sides.
static JPanel makeVerticalBoxPanel(int vMargin, int hMargin)
          Make a JPanel with a vertical BoxLayout; set the border to the given vertical/horizontal margins.
static JPanel makeVerticalBoxPanel(int topMargin, int leftMargin, int bottomMargin, int rightMargin)
          Make a JPanel with a vertical BoxLayout; set the border to the given margins.
static void onWindowClosed(Window w, Runnable r)
           
static void setAlignmentX(float a, JComponent... components)
          Set the horizontal alignment of the given components.
static void setAlignmentY(float a, JComponent... components)
          Set the vertical alignment of the given components.
static void setBackground(Color c, Component... components)
          Set the background color property of the given components.
static void setBorder(Border b, JComponent... components)
          Set the border of the given components.
static void setBottomAlignment(JComponent... components)
          Make the given components vertically-bottom aligned.
static void setDialogFont(int size, Component... components)
          Set the font of the given components to the logical "Dialog" font of the given size.
static void setDialogInputFont(int size, Component... components)
          Set the font of the given components to the logical "DialogInput" font of the given size.
static void setEmptyBorder(int top, int left, int bottom, int right, JComponent... components)
          Set the border of the given components to an empty border with the given margins.
static void setEmptyBorder(int vMargin, int hMargin, JComponent... components)
          Set the border of the given components to an empty border with the given vertical/horizontal margins.
static void setEmptyBorder(int margin, JComponent... components)
          Set the border of the given components to an empty border with the given margin.
static void setEnabled(boolean b, Component... components)
          Set the enabled property of the given components.
static void setFocusable(boolean b, Component... components)
          Set the focusable property of the given components.
static void setFont(Font f, Component... components)
          Set the font property of the given components.
static void setFont(String name, int size, Component... components)
          Set the font property of the given components.
static void setForeground(Color c, Component... components)
          Set the foreground color property of the given components.
static void setHorizontalCenterAlignment(JComponent... components)
          Make the given components horizontally-center aligned.
static void setLeftAlignment(JComponent... components)
          Make the given components horizontally-left aligned.
static void setLineBorder(JComponent... components)
          Set the border of the given components to a black line border.
static void setMaximumSize(Dimension d, JComponent... components)
          Set the maximum size of the given components.
static void setMaximumSize(int width, int height, JComponent... components)
          Set the maximum size of the given components.
static void setMinimumSize(Dimension d, JComponent... components)
          Set the minimum size of the given components.
static void setMinimumSize(int width, int height, JComponent... components)
          Set the minimum size of the given components.
static void setMonospacedFont(int size, Component... components)
          Set the font of the given components to the logical "Monospaced" font of the given size.
static void setOpaque(boolean b, JComponent... components)
          Set the opaque property of the given components.
static void setPopupLoc(Window popup)
          Convenience method for setPopupLoc(Window, Component) that gets the owner from popup.getOwner()
static void setPopupLoc(Window popup, Component owner)
          Sets the location of the popup in a consistent way.
static void setPreferredSize(Dimension d, JComponent... components)
          Set the preferred size of the given components.
static void setPreferredSize(int width, int height, JComponent... components)
          Set the preferred size of the given components.
static void setRightAlignment(JComponent... components)
          Make the given components horizontally-right aligned.
static void setSansSerifFont(int size, Component... components)
          Set the font of the given components to the logical "SansSerif" font of the given size.
static void setSerifFont(int size, Component... components)
          Set the font of the given components to the logical "Serif" font of the given size.
static void setTopAlignment(JComponent... components)
          Make the given components vertically-top aligned.
static void setVerticalCenterAlignment(JComponent... components)
          Make the given components vertically-center aligned.
static void setVisible(boolean b, Component... components)
          Set the visible property of the given components.
static void showApplet(Applet applet, int width, int height)
          Display the given applet in a window with title "Applet Viewer".
static void showApplet(String title, Applet applet, int width, int height)
          Display the given applet in a window with the given title.
static void showApplet(String title, Applet applet, int width, int height, URL root)
          Display the given applet in a window with the given title; if it is not null, sources are assumed to be rooted at the given URL.
static void showApplet(String title, Applet applet, int width, int height, URL root, Map<String,String> params)
          Display the given applet in a window with the given title; if it is not null, sources are assumed to be rooted at the given URL.
static void showApplet(String title, URL classPath, String className, int width, int height)
          Load the given applet class from the sources at URL classPath and display it in a window with the given title.
static void showApplet(String title, URL classPath, String className, int width, int height, Map<String,String> params)
          Load the given applet class from the sources at URL classPath and display it in a window with the given title.
static void showApplet(URL classPath, String className, int width, int height)
          Load the given applet class from the sources at URL classPath and display it in a window with title "Applet Viewer".
static void showDebug(String msg)
          Invoke showPopup(java.lang.String, java.lang.String) with title "Debug Message".
static void showPopup(String title, String msg)
          Show a modal message box with an OK button.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SwingUtil

public SwingUtil()
Method Detail

gray

public static Color gray(float degree)
Create a shade of gray with the given degree of darkness. 0.0f corresponds to white; 1.0f corresponds to black.


makeMainApplicationFrame

public static JFrame makeMainApplicationFrame(String title,
                                              int width,
                                              int height)
Boilerplate for creating a JFrame to be used as the main application window. Sets the title and preferred size to the given values. Sets the default close operation to "exit on close."


makeDisposableFrame

public static JFrame makeDisposableFrame(String title,
                                         int width,
                                         int height)
Boilerplate for creating a JFrame. Sets the title and preferred size to the given values. Sets the default close operation to "dispose on close."


makeReusableFrame

public static JFrame makeReusableFrame(String title,
                                       int width,
                                       int height)
Boilerplate for creating a JFrame. Sets the title and preferred size to the given values. Sets the default close operation to "hide on close."


onWindowClosed

public static void onWindowClosed(Window w,
                                  Runnable r)

displayWindow

public static void displayWindow(Window w)
Shortcut for the boilerplate code to pack and display a window (such as a JFrame).


makeHorizontalBoxPanel

public static JPanel makeHorizontalBoxPanel()
Make a JPanel with a horizontal BoxLayout.


makeHorizontalBoxPanel

public static JPanel makeHorizontalBoxPanel(int margin)
Make a JPanel with a horizontal BoxLayout; set the border to the given margin on all sides.


makeHorizontalBoxPanel

public static JPanel makeHorizontalBoxPanel(int vMargin,
                                            int hMargin)
Make a JPanel with a horizontal BoxLayout; set the border to the given vertical/horizontal margins.


makeHorizontalBoxPanel

public static JPanel makeHorizontalBoxPanel(int topMargin,
                                            int leftMargin,
                                            int bottomMargin,
                                            int rightMargin)
Make a JPanel with a horizontal BoxLayout; set the border to the given margins.


makeVerticalBoxPanel

public static JPanel makeVerticalBoxPanel()
Make a JPanel with a vertical BoxLayout.


makeVerticalBoxPanel

public static JPanel makeVerticalBoxPanel(int margin)
Make a JPanel with a vertical BoxLayout; set the border to the given margin on all sides.


makeVerticalBoxPanel

public static JPanel makeVerticalBoxPanel(int vMargin,
                                          int hMargin)
Make a JPanel with a vertical BoxLayout; set the border to the given vertical/horizontal margins.


makeVerticalBoxPanel

public static JPanel makeVerticalBoxPanel(int topMargin,
                                          int leftMargin,
                                          int bottomMargin,
                                          int rightMargin)
Make a JPanel with a vertical BoxLayout; set the border to the given margins.


makeBorderPanel

public static JPanel makeBorderPanel()
Make a JPanel with a BorderLayout.


makeBorderPanel

public static JPanel makeBorderPanel(int margin)
Make a JPanel with a BorderLayout; set the border to the given margin on all sides.


makeBorderPanel

public static JPanel makeBorderPanel(int vMargin,
                                     int hMargin)
Make a JPanel with a BorderLayout; set the border to the given vertical/horizontal margins.


makeBorderPanel

public static JPanel makeBorderPanel(int topMargin,
                                     int leftMargin,
                                     int bottomMargin,
                                     int rightMargin)
Make a JPanel with a BorderLayout; set the border to the given margins.


makeFlowPanel

public static JPanel makeFlowPanel()
Make a JPanel with a FlowLayout.


makeFlowPanel

public static JPanel makeFlowPanel(int margin)
Make a JPanel with a FlowLayout; set the border to the given margin on all sides.


makeFlowPanel

public static JPanel makeFlowPanel(int vMargin,
                                   int hMargin)
Make a JPanel with a FlowLayout; set the border to the given vertical/horizontal margins.


makeFlowPanel

public static JPanel makeFlowPanel(int topMargin,
                                   int leftMargin,
                                   int bottomMargin,
                                   int rightMargin)
Make a JPanel with a FlowLayout; set the border to the given margins.


add

public static void add(Container parent,
                       Component... children)
Add the given components to parent in sequence. Intended to reduce clutter in GUI code.


setBackground

public static void setBackground(Color c,
                                 Component... components)
Set the background color property of the given components. Intended to reduce clutter in GUI code.


setForeground

public static void setForeground(Color c,
                                 Component... components)
Set the foreground color property of the given components. Intended to reduce clutter in GUI code.


setBorder

public static void setBorder(Border b,
                             JComponent... components)
Set the border of the given components. Intended to reduce clutter in GUI code.


setEmptyBorder

public static void setEmptyBorder(int margin,
                                  JComponent... components)
Set the border of the given components to an empty border with the given margin.


setEmptyBorder

public static void setEmptyBorder(int vMargin,
                                  int hMargin,
                                  JComponent... components)
Set the border of the given components to an empty border with the given vertical/horizontal margins.


setEmptyBorder

public static void setEmptyBorder(int top,
                                  int left,
                                  int bottom,
                                  int right,
                                  JComponent... components)
Set the border of the given components to an empty border with the given margins.


setLineBorder

public static void setLineBorder(JComponent... components)
Set the border of the given components to a black line border.


setFont

public static void setFont(Font f,
                           Component... components)
Set the font property of the given components. Intended to reduce clutter in GUI code.


setFont

public static void setFont(String name,
                           int size,
                           Component... components)
Set the font property of the given components. Intended to reduce clutter in GUI code.


setSerifFont

public static void setSerifFont(int size,
                                Component... components)
Set the font of the given components to the logical "Serif" font of the given size.


setSansSerifFont

public static void setSansSerifFont(int size,
                                    Component... components)
Set the font of the given components to the logical "SansSerif" font of the given size.


setMonospacedFont

public static void setMonospacedFont(int size,
                                     Component... components)
Set the font of the given components to the logical "Monospaced" font of the given size.


setDialogFont

public static void setDialogFont(int size,
                                 Component... components)
Set the font of the given components to the logical "Dialog" font of the given size.


setDialogInputFont

public static void setDialogInputFont(int size,
                                      Component... components)
Set the font of the given components to the logical "DialogInput" font of the given size.


setEnabled

public static void setEnabled(boolean b,
                              Component... components)
Set the enabled property of the given components. Intended to reduce clutter in GUI code.


setFocusable

public static void setFocusable(boolean b,
                                Component... components)
Set the focusable property of the given components. Intended to reduce clutter in GUI code.


setVisible

public static void setVisible(boolean b,
                              Component... components)
Set the visible property of the given components. Intended to reduce clutter in GUI code.


setOpaque

public static void setOpaque(boolean b,
                             JComponent... components)
Set the opaque property of the given components. Intended to reduce clutter in GUI code.


setPreferredSize

public static void setPreferredSize(Dimension d,
                                    JComponent... components)
Set the preferred size of the given components. Intended to reduce clutter in GUI code. For Java 1.4 compatibility, the arguments must be JComponents, not arbitrary Components.


setPreferredSize

public static void setPreferredSize(int width,
                                    int height,
                                    JComponent... components)
Set the preferred size of the given components. Intended to reduce clutter in GUI code. For Java 1.4 compatibility, the arguments must be JComponents, not arbitrary Components.


setMaximumSize

public static void setMaximumSize(Dimension d,
                                  JComponent... components)
Set the maximum size of the given components. Intended to reduce clutter in GUI code. For Java 1.4 compatibility, the arguments must be JComponents, not arbitrary Components.


setMaximumSize

public static void setMaximumSize(int width,
                                  int height,
                                  JComponent... components)
Set the maximum size of the given components. Intended to reduce clutter in GUI code. For Java 1.4 compatibility, the arguments must be JComponents, not arbitrary Components.


setMinimumSize

public static void setMinimumSize(Dimension d,
                                  JComponent... components)
Set the minimum size of the given components. Intended to reduce clutter in GUI code. For Java 1.4 compatibility, the arguments must be JComponents, not arbitrary Components.


setMinimumSize

public static void setMinimumSize(int width,
                                  int height,
                                  JComponent... components)
Set the minimum size of the given components. Intended to reduce clutter in GUI code. For Java 1.4 compatibility, the arguments must be JComponents, not arbitrary Components.


setLeftAlignment

public static void setLeftAlignment(JComponent... components)
Make the given components horizontally-left aligned.


setRightAlignment

public static void setRightAlignment(JComponent... components)
Make the given components horizontally-right aligned.


setHorizontalCenterAlignment

public static void setHorizontalCenterAlignment(JComponent... components)
Make the given components horizontally-center aligned.


setTopAlignment

public static void setTopAlignment(JComponent... components)
Make the given components vertically-top aligned.


setBottomAlignment

public static void setBottomAlignment(JComponent... components)
Make the given components vertically-bottom aligned.


setVerticalCenterAlignment

public static void setVerticalCenterAlignment(JComponent... components)
Make the given components vertically-center aligned.


setAlignmentX

public static void setAlignmentX(float a,
                                 JComponent... components)
Set the horizontal alignment of the given components. Intended to reduce clutter in GUI code.


setAlignmentY

public static void setAlignmentY(float a,
                                 JComponent... components)
Set the vertical alignment of the given components. Intended to reduce clutter in GUI code.


invokeLater

public static void invokeLater(Runnable task)
Runs the task synchronously if the current thread is the event thread; otherwise passes it to the event thread to be run asynchronously after all events already on the queue have been processed.


invokeAndWait

public static void invokeAndWait(Runnable task)
Runs the the task in the event thread, blocking until it is complete.

Throws:
WrappedException - Wrapping an InterruptedException if the current thread is interrupted
RuntimeException - If an exception is thrown by task

clearEventQueue

public static void clearEventQueue()
                            throws InterruptedException
Wait for all items in the event queue to be handled. This thread will block until all items currently on the event queue have been handled.

Throws:
IllegalStateException - If this is the event dispatch thread (it is impossible to wait for the event queue to clear if this code is running in the event queue).
InterruptedException - If this thread is interrupted while waiting.

attemptClearEventQueue

public static void attemptClearEventQueue()
Call @link{#clearEventQueue}, but ignore any resulting InterruptedException. This method does not guarantee that the queue will actually be cleared.

Throws:
IllegalStateException - If this is the event dispatch thread (it is impossible to wait for the event queue to clear if this code is running in the event queue).

asActionListener

public static ActionListener asActionListener(Runnable r)
Convert a Runnable to an ActionListener


asActionListener

public static ActionListener asActionListener(Runnable1<? super ActionEvent> r)
Convert a Runnable1 to an ActionListener


asRunnable

public static Runnable1<ActionEvent> asRunnable(ActionListener l)
Convert an ActionListener to a Runnable1


asSwingFileFilter

public static FileFilter asSwingFileFilter(FileFilter filter,
                                           String description)
Define a Swing file filter (for use with JFileChoosers) in terms if a FileFilter.


asSwingFileFilter

public static FileFilter asSwingFileFilter(Predicate<? super File> p,
                                           String description)
Define a Swing file filter (for use with JFileChoosers) in terms if a FileFilter.


asSwingFileFilter

public static FileFilter asSwingFileFilter(FilePredicate p,
                                           String description)
Define a Swing file filter (for use with JFileChoosers) in terms if a FileFilter. (Defined to disambiguate, since both other declarations apply to FilePredicates.)


disposeAction

public static ActionListener disposeAction(Window w)
Create an action to invoke Window.dispose() on the given window


showDebug

public static void showDebug(String msg)
Invoke showPopup(java.lang.String, java.lang.String) with title "Debug Message". This may be called by any thread.


showPopup

public static void showPopup(String title,
                             String msg)
Show a modal message box with an OK button. This may be called by any thread.

Parameters:
msg - String to display
title - Title of the box

showApplet

public static void showApplet(URL classPath,
                              String className,
                              int width,
                              int height)
                       throws ReflectException
Load the given applet class from the sources at URL classPath and display it in a window with title "Applet Viewer".

Throws:
ReflectException

showApplet

public static void showApplet(String title,
                              URL classPath,
                              String className,
                              int width,
                              int height)
                       throws ReflectException
Load the given applet class from the sources at URL classPath and display it in a window with the given title.

Throws:
ReflectException

showApplet

public static void showApplet(String title,
                              URL classPath,
                              String className,
                              int width,
                              int height,
                              Map<String,String> params)
                       throws ReflectException
Load the given applet class from the sources at URL classPath and display it in a window with the given title.

Throws:
ReflectException

showApplet

public static void showApplet(Applet applet,
                              int width,
                              int height)
Display the given applet in a window with title "Applet Viewer".


showApplet

public static void showApplet(String title,
                              Applet applet,
                              int width,
                              int height)
Display the given applet in a window with the given title.


showApplet

public static void showApplet(String title,
                              Applet applet,
                              int width,
                              int height,
                              URL root)
Display the given applet in a window with the given title; if it is not null, sources are assumed to be rooted at the given URL.


showApplet

public static void showApplet(String title,
                              Applet applet,
                              int width,
                              int height,
                              URL root,
                              Map<String,String> params)
Display the given applet in a window with the given title; if it is not null, sources are assumed to be rooted at the given URL.


getClipboardSelection

public static String getClipboardSelection(Component c)
Returns:
a string with the current clipboard selection, or null if not available.

getAction

public static Action getAction(JTextComponent component,
                               String actionName)
Search the given text component's list of actions for an action with the given name. Returns null if no such action is found. See DefaultEditorKit for a list of action name constants.


getActions

public static Map<String,Action> getActions(JTextComponent component)
Create a map view of component.getActions(), where the keys are the action names. See DefaultEditorKit for a list of action name constants.


setPopupLoc

public static void setPopupLoc(Window popup)
Convenience method for setPopupLoc(Window, Component) that gets the owner from popup.getOwner()


setPopupLoc

public static void setPopupLoc(Window popup,
                               Component owner)

Sets the location of the popup in a consistent way. If the popup has an owner, the popup is centered over the owner. If the popup has no owner (owner == null), the popup is centered over the first monitor. In either case, the popup is moved and scaled if any part of it is not on the screen. This method should be called for all popups to maintain consistancy.

This method should only be called from the event thread.

Parameters:
popup - the popup window
owner - the parent component for the popup, or null