Holger's
Java API

com.antelmann.util.gui
Class Menus

java.lang.Object
  extended by com.antelmann.util.gui.Menus

public final class Menus
extends Object

this class provides some convenient menus, dialogs and other functions (additions to SwingUtilities) that are potentially useful for any Java Swing Application. If available, it takes advantage of com.incors.plaf.kunststoff.KunststoffLookAndFeel and com.jgoodies.looks.

Author:
Holger Antelmann

Field Summary
static String aboutString
           
static String EXCEPTION_KEY
          used to put the thrown Exception as a value into the specialExceptionAction
 
Constructor Summary
Menus()
           
 
Method Summary
static void addActionToMenu(JMenu menu, Action action, String menuKey, String menuDelimiter)
          adds the given action to the given menu
static void addActionToMenu(JMenuBar menuBar, Action action, String menuKey, String menuDelimiter)
          adds the given action to the given menu bar
static Appendable asAppendable(JTextArea ta)
           
static void bringToFront(Component c)
          attempts several things to bring the component to the front w/ focus; works well for all components that either are or have a parent of the type Window or JInternalFrame
static void cascadeInternalFrames(JDesktopPane desktop)
           
static void center(Component c)
          centers the given Component in the middle of the screen
static void clickOnKey(AbstractButton button, KeyStroke keyStroke, Object actionKey)
           
static boolean confirm(Component parent, Object message)
           
static JMenuItem createAboutBoxMenuItem(Frame parent)
          returns an 'about' menu capable of displaying an Antelmann.com's info box.
static JMenuItem createAboutDialogMenuItem(Frame parent)
          returns an 'about' menu capable of displaying an Antelmann.com's info box.
static JPopupMenu createCopyPasteMenu(JTextComponent comp)
           
static JMenuBar createDefaultMenuBar(Frame parent)
          returns a convenient standard JMenuBar to be used with any Frame
static JDialog createDialog(Component parent, String title, boolean modal)
           
static JMenu createLookAndFeelMenu(Component rootComponent)
          createLookAndFeelMenu() generates a JMenu that lets the user select from different look&feel options.
static JMenu createLookAndFeelMenu(Component component, boolean searchForRootAtRuntime)
           
static ValueRetriever<JPopupMenu> createUndoMenuRetriever(UndoManager manager)
           
static boolean disposeComponent(Component c)
           
static void enableActionOnKey(JComponent component, KeyStroke keyStroke, ActionListener al, Object actionKey)
           
static void enableCopyPasteKeys(JComponent... comps)
          enables Ctrl-X, Ctrl-C, Ctrl-V for the given component
static void enableCopyPasteMenu(JTextComponent... comps)
           
static void enableMouseContextMenu(Component comp, JPopupMenu menu)
          adds a MouseListener to the given Component that activates the given menu upon right-mouse-click
static void enableMouseContextMenu(Component comp, ValueRetriever<JPopupMenu> retriever)
          useful if the menu is really to be created at the time of invocation
static UndoManager enableUndo(JTextComponent tc)
          enables undo with 'Ctrl-z' and redo with 'Ctrl-y'
static
<T extends Component>
ArrayList<T>
findComponents(Class<T> type, Component comp)
           
static
<T extends Component>
T
findFirstComponent(Class<T> type, Component comp)
           
static ImageIcon getAntelmannIcon()
           
static JComponent getComponentFor(Object object)
           
static Dialog getDialogForComponent(Component component)
           
static Box.Filler getFiller(int width, int height)
           
static ResourceBundle getLanguage(Locale locale)
           
static String[] getLookAndFeelNames()
           
static Dimension getScaledScreenSize(Component c, double xfactor, double yfactor)
           
static Action getSpecialExceptionAction()
           
static boolean isCapsLockOn()
           
static ResourceBundle language()
          provides access to the Locale specific language used in this package
static void makeCompactSpringGrid(Container parent, int rows, int cols, int padding)
           
static void makeCompactSpringGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad, int yPad)
          Aligns the first rows * cols components of parent in a grid.
static void makeFloat(JToolBar toolbar)
          centers the given toolbar floating on the center of the component that contains the toolbar
static void makeFloat(JToolBar toolbar, int x, int y)
          makes the toolbar floating (as if later dragged with the mouse); call this only after the component was added to a window.
static void makeSpringGrid(Container parent, int rows, int cols, int padding)
           
static void makeSpringGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad, int yPad)
          Aligns the first rows * cols components of parent in a grid.
static JFrame makeVisibleOffscreenFrame()
          creates a frame that is drawn offscreen but visible for providing a default component for message boxes (needs to be disposed hereafter)
static
<T extends Component>
T
resizePreferred(T comp, double xfactor, double yfactor)
           
static void selectTabFor(Component c)
           
static void setCapsLock(boolean flag)
           
static void setLanguage(Locale locale)
           
static void setLightWeight(boolean flag)
          for reference; this is true by default (but also can be set for each JComponent separately)
static boolean setLookAndFeel(String name)
           
static boolean setPlatformLookAndFeel()
           
static void setSpecialExceptionAction(Action action)
          allows to register a special action that is provided as a button when displaying an Exception
static boolean setStandardLookAndFeel()
           
static void showAboutBox(Component parent)
          shows a small antelmann.com about dialog
static void showAboutDialog(Component parent)
          shows the standard antelmann.com about dialog with license, version and upgrade button
static int showConfirmDialog(Component parent, String title, Object message, boolean resizable, int messageType)
           
static int showDialog(Component parent, String title, Object message, boolean resizable, int optionType, int messageType)
           
static void showExceptionDialog(Component parent, Throwable ex)
           
static void showExceptionDialog(Component parent, Throwable ex, Thread t)
          if the given Throwable is an instance of WarningOnly, showWarning(Component, Throwable) is called
static void showExceptionDialog(Throwable ex)
           
static void showLicenseDialog(Component parent)
           
static int showMessageDialog(Component parent, String title, Object message, boolean resizable, int messageType)
           
static void showPropertiesDialog(String title, Map<?,?> properties, Component parent, boolean modal)
          Deprecated. use makePropertiesTable with JOptionPane instead
static void showVersionDialog(Component parent)
           
static void showWarning(Component parent, Throwable ex)
           
static void sizeToScreen(Component c, double xfactor, double yfactor)
          sets the size of the given component relative to the screen size
static boolean useLookAndFeelName(String name)
           
static Component wrapToScreen(Component c)
           
static Component wrapToScreen(Component c, double widthFactor, double heightFactor)
          returns either the given Component or a JScrollPane that is sized appropriately
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

aboutString

public static final String aboutString

EXCEPTION_KEY

public static final String EXCEPTION_KEY
used to put the thrown Exception as a value into the specialExceptionAction

See Also:
setSpecialExceptionAction(Action), Constant Field Values
Constructor Detail

Menus

public Menus()
Method Detail

getLookAndFeelNames

public static String[] getLookAndFeelNames()
See Also:
setLookAndFeel(String)

setLookAndFeel

public static boolean setLookAndFeel(String name)
See Also:
UIManager.LookAndFeelInfo.getName()

setStandardLookAndFeel

public static boolean setStandardLookAndFeel()

setPlatformLookAndFeel

public static boolean setPlatformLookAndFeel()

getAntelmannIcon

public static ImageIcon getAntelmannIcon()

getLanguage

public static ResourceBundle getLanguage(Locale locale)

language

public static ResourceBundle language()
provides access to the Locale specific language used in this package


setLanguage

public static void setLanguage(Locale locale)

setLightWeight

public static void setLightWeight(boolean flag)
for reference; this is true by default (but also can be set for each JComponent separately)

See Also:
JComboBox.setLightWeightPopupEnabled(boolean), JPopupMenu.setLightWeightPopupEnabled(boolean)

makeFloat

public static void makeFloat(JToolBar toolbar)
centers the given toolbar floating on the center of the component that contains the toolbar

See Also:
makeFloat(JToolBar, int, int)

makeFloat

public static void makeFloat(JToolBar toolbar,
                             int x,
                             int y)
makes the toolbar floating (as if later dragged with the mouse); call this only after the component was added to a window. Note that when invoking this method in AncestorListener.ancestorAdded(AncestorEvent), you must ensure that this method is only executed once, since this method itself will invoke the listener's method.

Parameters:
toolbar - the toolbar that is to made floating
x - horizontal offset from the top left screen edge defining where the toolbar should float
y - vertical offset from the top left screen edge defining where the toolbar should float

center

public static void center(Component c)
centers the given Component in the middle of the screen

See Also:
Window.setLocationRelativeTo(Component)

resizePreferred

public static <T extends Component> T resizePreferred(T comp,
                                                      double xfactor,
                                                      double yfactor)

sizeToScreen

public static void sizeToScreen(Component c,
                                double xfactor,
                                double yfactor)
sets the size of the given component relative to the screen size

Parameters:
c - the component to be resized
xfactor - the width relative to the screen; 0.5 will set the width to half the screen width
yfactor - the height relative to the screen; 0.5 will set the height to half the screen height

getScaledScreenSize

public static Dimension getScaledScreenSize(Component c,
                                            double xfactor,
                                            double yfactor)
Parameters:
c - the component that may specify which screen we are on (if we had multiple screens)

createDefaultMenuBar

public static JMenuBar createDefaultMenuBar(Frame parent)
returns a convenient standard JMenuBar to be used with any Frame


makeVisibleOffscreenFrame

public static JFrame makeVisibleOffscreenFrame()
creates a frame that is drawn offscreen but visible for providing a default component for message boxes (needs to be disposed hereafter)


selectTabFor

public static void selectTabFor(Component c)

bringToFront

public static void bringToFront(Component c)
attempts several things to bring the component to the front w/ focus; works well for all components that either are or have a parent of the type Window or JInternalFrame

Parameters:
c - the component to be made restored, visible on top and having focus

createLookAndFeelMenu

public static JMenu createLookAndFeelMenu(Component rootComponent)
createLookAndFeelMenu() generates a JMenu that lets the user select from different look&feel options.

Parameters:
rootComponent - the top component of the GUI tree that needs to be updated

createLookAndFeelMenu

public static JMenu createLookAndFeelMenu(Component component,
                                          boolean searchForRootAtRuntime)

useLookAndFeelName

public static boolean useLookAndFeelName(String name)

createAboutDialogMenuItem

public static JMenuItem createAboutDialogMenuItem(Frame parent)
returns an 'about' menu capable of displaying an Antelmann.com's info box.

Parameters:
parent - the component over which the modal dialog is to be placed

createAboutBoxMenuItem

public static JMenuItem createAboutBoxMenuItem(Frame parent)
returns an 'about' menu capable of displaying an Antelmann.com's info box.

Parameters:
parent - the component over which the modal dialog is to be placed

showAboutBox

public static void showAboutBox(Component parent)
shows a small antelmann.com about dialog


showAboutDialog

public static void showAboutDialog(Component parent)
shows the standard antelmann.com about dialog with license, version and upgrade button


showVersionDialog

public static void showVersionDialog(Component parent)

showLicenseDialog

public static void showLicenseDialog(Component parent)

showPropertiesDialog

@Deprecated
public static void showPropertiesDialog(String title,
                                                   Map<?,?> properties,
                                                   Component parent,
                                                   boolean modal)
Deprecated. use makePropertiesTable with JOptionPane instead

shows a JDialog containing a JTable to display the given properties sorted

See Also:
TableUtils.makePropertiesTable(Map, String, String)

getSpecialExceptionAction

public static Action getSpecialExceptionAction()

setSpecialExceptionAction

public static void setSpecialExceptionAction(Action action)
allows to register a special action that is provided as a button when displaying an Exception

See Also:
showExceptionDialog(Component, Throwable, Thread), EXCEPTION_KEY

showExceptionDialog

public static void showExceptionDialog(Throwable ex)

showExceptionDialog

public static void showExceptionDialog(Component parent,
                                       Throwable ex)

showExceptionDialog

public static void showExceptionDialog(Component parent,
                                       Throwable ex,
                                       Thread t)
if the given Throwable is an instance of WarningOnly, showWarning(Component, Throwable) is called

See Also:
WarningOnly

showWarning

public static void showWarning(Component parent,
                               Throwable ex)

confirm

public static boolean confirm(Component parent,
                              Object message)

showConfirmDialog

public static int showConfirmDialog(Component parent,
                                    String title,
                                    Object message,
                                    boolean resizable,
                                    int messageType)

showMessageDialog

public static int showMessageDialog(Component parent,
                                    String title,
                                    Object message,
                                    boolean resizable,
                                    int messageType)

showDialog

public static int showDialog(Component parent,
                             String title,
                             Object message,
                             boolean resizable,
                             int optionType,
                             int messageType)

createDialog

public static JDialog createDialog(Component parent,
                                   String title,
                                   boolean modal)
See Also:
JOptionPane.createDialog(Component, String)

getDialogForComponent

public static Dialog getDialogForComponent(Component component)

setCapsLock

public static void setCapsLock(boolean flag)
                        throws UnsupportedOperationException
Throws:
UnsupportedOperationException

isCapsLockOn

public static boolean isCapsLockOn()
                            throws UnsupportedOperationException
Throws:
UnsupportedOperationException

makeSpringGrid

public static void makeSpringGrid(Container parent,
                                  int rows,
                                  int cols,
                                  int padding)

makeSpringGrid

public static void makeSpringGrid(Container parent,
                                  int rows,
                                  int cols,
                                  int initialX,
                                  int initialY,
                                  int xPad,
                                  int yPad)
Aligns the first rows * cols components of parent in a grid. Each component is as big as the maximum preferred width and height of the components. The parent is made just big enough to fit them all. The parent component must be a SpringLayout. The code for this method is based on the tutorial from the Sun Java website.

Parameters:
rows - number of rows; if 0 is given, the number of rows is the number of components by the container divided by the number of columns; note, however, that the component must contain exactly rows * columns components!
cols - number of columns
initialX - x location to start the grid at
initialY - y location to start the grid at
xPad - x padding between cells
yPad - y padding between cells

makeCompactSpringGrid

public static void makeCompactSpringGrid(Container parent,
                                         int rows,
                                         int cols,
                                         int padding)
See Also:
makeCompactSpringGrid(Container, int, int, int, int, int, int)

makeCompactSpringGrid

public static void makeCompactSpringGrid(Container parent,
                                         int rows,
                                         int cols,
                                         int initialX,
                                         int initialY,
                                         int xPad,
                                         int yPad)
Aligns the first rows * cols components of parent in a grid. Each component in a column is as wide as the maximum preferred width of the components in that column; height is similarly determined for each row. The parent is made just big enough to fit them all. The parent component must be a SpringLayout. The code for this method is based on the tutorial from the Sun Java website.

Parameters:
rows - number of rows; if 0 is given, the number of rows is the number of components by the container divided by the number of columns. If there are fewer components than given by the product of rows and colums, filler components are automatically used.
cols - number of columns
initialX - x location to start the grid at
initialY - y location to start the grid at
xPad - x padding between cells
yPad - y padding between cells
See Also:
SpringLayout

getFiller

public static Box.Filler getFiller(int width,
                                   int height)

enableUndo

public static UndoManager enableUndo(JTextComponent tc)
enables undo with 'Ctrl-z' and redo with 'Ctrl-y'


createUndoMenuRetriever

public static ValueRetriever<JPopupMenu> createUndoMenuRetriever(UndoManager manager)

enableCopyPasteKeys

public static void enableCopyPasteKeys(JComponent... comps)
enables Ctrl-X, Ctrl-C, Ctrl-V for the given component

See Also:
TransferHandler.getCopyAction(), TransferHandler.getPasteAction(), TransferHandler.getCutAction()

enableCopyPasteMenu

public static void enableCopyPasteMenu(JTextComponent... comps)

createCopyPasteMenu

public static JPopupMenu createCopyPasteMenu(JTextComponent comp)

enableMouseContextMenu

public static void enableMouseContextMenu(Component comp,
                                          JPopupMenu menu)
adds a MouseListener to the given Component that activates the given menu upon right-mouse-click

See Also:
JComponent.setComponentPopupMenu(JPopupMenu)

enableMouseContextMenu

public static void enableMouseContextMenu(Component comp,
                                          ValueRetriever<JPopupMenu> retriever)
useful if the menu is really to be created at the time of invocation


wrapToScreen

public static Component wrapToScreen(Component c)

wrapToScreen

public static Component wrapToScreen(Component c,
                                     double widthFactor,
                                     double heightFactor)
returns either the given Component or a JScrollPane that is sized appropriately


addActionToMenu

public static void addActionToMenu(JMenuBar menuBar,
                                   Action action,
                                   String menuKey,
                                   String menuDelimiter)
adds the given action to the given menu bar

Parameters:
menuBar - the menu bar to which the action is to be added
action - the action to be added
menuKey - specifies the menu name to be used to place the action; if null or empty, the action is not placed at all
menuDelimiter - specifies a delimiter that allows to specify a hierarchy of sub-menus with the menuKey parameter

addActionToMenu

public static void addActionToMenu(JMenu menu,
                                   Action action,
                                   String menuKey,
                                   String menuDelimiter)
adds the given action to the given menu

Parameters:
menu - the menu to which the action is to be added
action - the action to be added
menuKey - allows to specify a submenu under which the given action is to be placed within the structure
menuDelimiter - specifies a delimiter that allows to specify a hierarchy of sub-menus with the menuKey parameter

getComponentFor

public static JComponent getComponentFor(Object object)

enableActionOnKey

public static void enableActionOnKey(JComponent component,
                                     KeyStroke keyStroke,
                                     ActionListener al,
                                     Object actionKey)

clickOnKey

public static void clickOnKey(AbstractButton button,
                              KeyStroke keyStroke,
                              Object actionKey)

findFirstComponent

public static <T extends Component> T findFirstComponent(Class<T> type,
                                                         Component comp)

findComponents

public static <T extends Component> ArrayList<T> findComponents(Class<T> type,
                                                                Component comp)

asAppendable

public static Appendable asAppendable(JTextArea ta)

cascadeInternalFrames

public static void cascadeInternalFrames(JDesktopPane desktop)

disposeComponent

public static boolean disposeComponent(Component c)


(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads