Class DialogUtils


  • public class DialogUtils
    extends Object
    Static utility methods for common dialog and GUI related tasks.
    Since:
    2.7
    Author:
    Michael Bedward
    • Constructor Summary

      Constructors 
      Constructor Description
      DialogUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T extends JComponent>
      List<T>
      getChildComponents​(Class<T> clazz, Container parent, boolean includeNested)
      Gets all child components that are, or derive from, the given class.
      static Dimension getHtmlLabelTextExtent​(String labelText, int fixedDimSize, boolean width)
      Calculates the dimensions that a given text string requires when rendered as HTML text in a label component.
      static String getString​(String input, String fallback)
      Returns input if not null or empty, otherwise returns fallback.
      static void showCentred​(Window dialog)
      Shows a dialog centred on the screen.
      static void showCentredOnParent​(Window parent, Window dialog)
      Shows a dialog centred on its parent.
      • Methods inherited from class Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DialogUtils

        public DialogUtils()
    • Method Detail

      • showCentred

        public static void showCentred​(Window dialog)
        Shows a dialog centred on the screen. May be called safely from any thread.
        Parameters:
        dialog - the dialog
      • showCentredOnParent

        public static void showCentredOnParent​(Window parent,
                                               Window dialog)
        Shows a dialog centred on its parent. May be called safely from any thread. If parent is null the dialog is centred on the screen.
        Parameters:
        parent - the parent component
        dialog - the dialog
      • getChildComponents

        public static <T extends JComponent> List<T> getChildComponents​(Class<T> clazz,
                                                                        Container parent,
                                                                        boolean includeNested)
        Gets all child components that are, or derive from, the given class. This method is adapted from the SwingUtils class written by Darryl Burke. (Accessed from: http://tips4java.wordpress.com/2008/11/13/swing-utils/).
        Type Parameters:
        T - Swing type derived from JComponent
        Parameters:
        clazz - the component class
        parent - the parent container
        includeNested - whether to recursively collect nested components
        Returns:
        list of child components
      • getString

        public static String getString​(String input,
                                       String fallback)
        Returns input if not null or empty, otherwise returns fallback. This is handy for setting dialog titles etc. Note that the input string is considered empty if input.trim().length() == 0.
        Parameters:
        input - input string
        fallback - fallback string (may be null)
        Returns:
        input unless it is null or empty, in which case fallback is returned
      • getHtmlLabelTextExtent

        public static Dimension getHtmlLabelTextExtent​(String labelText,
                                                       int fixedDimSize,
                                                       boolean width)
        Calculates the dimensions that a given text string requires when rendered as HTML text in a label component.

        The method used is adapted from that described in a blog post by Morten Nobel:

        http://blog.nobel-joergensen.com/2009/01/18/changing-preferred-size-of-a-html-jlabel/
        Parameters:
        labelText - the text to render, optionally enclosed in <html>...</html> tags
        fixedDimSize - the size of the fixed dimension (either width or height
        width - true if the fixed dimension is width; false for height
        Returns:
        the rendered label text extent