Class ComponentHelper

java.lang.Object
net.simplace.ui.service.ComponentHelper

public final class ComponentHelper extends Object
Helper for GUI components.
Author:
Andreas Enders
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Centers the given window relative to its parent.
    static Color
    combineColors(Color aColor1, Color aColor2)
    Mixes two colors by averaging their RGB components.
    static ImageIcon
    combineIcons(ImageIcon aIcon, int aIconX, int aIconY, ImageIcon aCoIcon, int aCoIconX, int aCoIconY)
    Combines a main icon with an overlay icon (e.g.
    static ImageIcon
    combineIcons(ImageIcon aIcon, int aIconX, int aIconY, ImageIcon aCoIcon, int aCoIconX, int aCoIconY, boolean aFadeIcon)
    Combines a main icon with an overlay icon (e.g.
    static Color
    createColor(String aColorString)
    Creates an RGB color from a string like 127,0,0 or 127|0|0.
    static ImageIcon
    Converts an icon to grey scale, e.g.
    static JTextArea
    Searches the component tree depth-first for the first descendant of type JTextArea.
    static void
    fitColumnWidth(JTable aTable, int aMaxWidth, boolean aResizable)
    Sets the width of all columns of a table to fit their header and cell contents.
    static int
    fitColumnWidth(JTable aTable, int aColumnIndex, int aMaxWidth, boolean aResizable)
    Sets the width of a column to fit its header and cell contents.
    static int
    fitColumnWidth(JTable aTable, int aColumnIndex, int aMinWidth, int aMaxWidth, boolean aResizable)
    Sets the width of a column to fit its header and cell contents.
    getColumn(JTable aTable, int aModelIndex)
    Returns the table column for a model column index, regardless of the column's current position in the view.
    static Component
    getComponentByBounds(Container aParent, int aX, int aY)
    Returns the deepest descendant component that contains the given point.
    static boolean
    isAnchestor(Component aComponent, Component aAnchestorComponent)
    Checks whether the second component is an ancestor of the first component, i.e.
    static boolean
    Checks if the color depth of the component's toolkit is at least 24 bit.
    static void
    Sets the default size and location of a window: 1024x748 pixels centered if the usable screen area is larger, otherwise the whole usable screen area.
    static void
    setHeight(JComponent aComponent, int aHeight)
    Sets the minimum and preferred height of a component, keeping its widths.
    static Color
    transformColor(Color aColor, double aRelativeChangeValue)
    Returns a brighter or darker color by multiplying each RGB component with a factor.
    static Color
    transformColor(Color aColor, int aAbsoluteChangeValue)
    Returns a brighter or darker color by adding a value to each RGB component.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isAnchestor

      public static boolean isAnchestor(Component aComponent, Component aAnchestorComponent)
      Checks whether the second component is an ancestor of the first component, i.e. whether it is found when walking up the parent chain of the first component. A component counts as its own ancestor.
      Parameters:
      aComponent - the component whose parent chain is searched
      aAnchestorComponent - the possible ancestor
      Returns:
      true if aAnchestorComponent is aComponent or one of its ancestors
    • findFirstJTextArea

      public static JTextArea findFirstJTextArea(JComponent aComponent)
      Searches the component tree depth-first for the first descendant of type JTextArea.
      Parameters:
      aComponent - root of the search; not checked itself
      Returns:
      the first descendant of type JTextArea, or null if there is none
    • getColumn

      public static TableColumn getColumn(JTable aTable, int aModelIndex)
      Returns the table column for a model column index, regardless of the column's current position in the view.
      Parameters:
      aTable - the table
      aModelIndex - column index in the table model
      Returns:
      the table column, or null if no column shows this model index
    • fitColumnWidth

      public static void fitColumnWidth(JTable aTable, int aMaxWidth, boolean aResizable)
      Sets the width of all columns of a table to fit their header and cell contents.
      Parameters:
      aTable - the table
      aMaxWidth - maximum width of a column in pixels
      aResizable - if false, the columns are fixed to the calculated width
    • fitColumnWidth

      public static int fitColumnWidth(JTable aTable, int aColumnIndex, int aMaxWidth, boolean aResizable)
      Sets the width of a column to fit its header and cell contents.
      Parameters:
      aTable - the table
      aColumnIndex - column index in the table model
      aMaxWidth - maximum width of the column in pixels
      aResizable - if false, the column is fixed to the calculated width
      Returns:
      the calculated column width in pixels
    • fitColumnWidth

      public static int fitColumnWidth(JTable aTable, int aColumnIndex, int aMinWidth, int aMaxWidth, boolean aResizable)
      Sets the width of a column to fit its header and cell contents.
      Parameters:
      aTable - the table
      aColumnIndex - column index in the table model
      aMinWidth - minimum width of the column in pixels
      aMaxWidth - maximum width of the column in pixels
      aResizable - if false, the column is fixed to the calculated width
      Returns:
      the calculated column width in pixels
    • transformColor

      public static Color transformColor(Color aColor, int aAbsoluteChangeValue)
      Returns a brighter or darker color by adding a value to each RGB component. The results are limited to 0..255.
      Parameters:
      aColor - the original color
      aAbsoluteChangeValue - value added to each component; positive values brighten, negative values darken
      Returns:
      the transformed color
    • transformColor

      public static Color transformColor(Color aColor, double aRelativeChangeValue)
      Returns a brighter or darker color by multiplying each RGB component with a factor. The results are limited to 0..255.
      Parameters:
      aColor - the original color
      aRelativeChangeValue - factor: < 1.0 darker, 1.0 unchanged, > 1.0 brighter
      Returns:
      the transformed color
    • isToolkitTrueColor

      public static boolean isToolkitTrueColor(Component aComponent)
      Checks if the color depth of the component's toolkit is at least 24 bit.
      Parameters:
      aComponent - component whose toolkit is checked
      Returns:
      true if the color depth is at least 24 bit
    • setDefaultWindowBounds

      public static void setDefaultWindowBounds(Window aWindow)
      Sets the default size and location of a window: 1024x748 pixels centered if the usable screen area is larger, otherwise the whole usable screen area.
      Parameters:
      aWindow - the window to size and position
    • getComponentByBounds

      public static Component getComponentByBounds(Container aParent, int aX, int aY)
      Returns the deepest descendant component that contains the given point.
      Parameters:
      aParent - container to search in
      aX - x coordinate relative to aParent
      aY - y coordinate relative to aParent
      Returns:
      the deepest component containing the point, or null if no child contains it
    • centerWindow

      public static void centerWindow(Window aWindow)
      Centers the given window relative to its parent. If no parent is available, the window is centered on the screen.
      Parameters:
      aWindow - the window to center
    • combineIcons

      public static ImageIcon combineIcons(ImageIcon aIcon, int aIconX, int aIconY, ImageIcon aCoIcon, int aCoIconX, int aCoIconY)
      Combines a main icon with an overlay icon (e.g. a status symbol).
      Parameters:
      aIcon - the main icon
      aIconX - x offset of the main icon
      aIconY - y offset of the main icon
      aCoIcon - the overlay icon, drawn aligned to the bottom edge
      aCoIconX - x offset of the overlay icon
      aCoIconY - extra height added to the image for the overlay icon
      Returns:
      the combined icon
      See Also:
    • combineIcons

      public static ImageIcon combineIcons(ImageIcon aIcon, int aIconX, int aIconY, ImageIcon aCoIcon, int aCoIconX, int aCoIconY, boolean aFadeIcon)
      Combines a main icon with an overlay icon (e.g. a status symbol). If the overlay icon is at least as large as the main icon, the main icon is moved 3 pixels to the right.
      Parameters:
      aIcon - the main icon
      aIconX - x offset of the main icon
      aIconY - y offset of the main icon
      aCoIcon - the overlay icon, drawn aligned to the bottom edge
      aCoIconX - x offset of the overlay icon
      aCoIconY - extra height added to the image for the overlay icon
      aFadeIcon - if true, the main icon is converted to grey scale first
      Returns:
      the combined icon
    • combineColors

      public static Color combineColors(Color aColor1, Color aColor2)
      Mixes two colors by averaging their RGB components.
      Parameters:
      aColor1 - first color
      aColor2 - second color
      Returns:
      the mixed color
    • createColor

      public static Color createColor(String aColorString)
      Creates an RGB color from a string like 127,0,0 or 127|0|0.
      Parameters:
      aColorString - red, green and blue values (0..255), separated by comma or pipe
      Returns:
      the color, or Color.RED if a value is not a number
    • setHeight

      public static void setHeight(JComponent aComponent, int aHeight)
      Sets the minimum and preferred height of a component, keeping its widths.
      Parameters:
      aComponent - the component
      aHeight - height in pixels
    • fadeIconColors

      public static ImageIcon fadeIconColors(ImageIcon aImageIcon)
      Converts an icon to grey scale, e.g. to show it as disabled.
      Parameters:
      aImageIcon - the original icon
      Returns:
      a new grey scale icon