Class WindowPlacement

java.lang.Object
edu.cnu.mdi.swing.WindowPlacement

public final class WindowPlacement extends Object
Utility class for common window and component placement tasks in Swing applications.

This class centralizes logic for determining the "main" display screen, centering windows, sizing components relative to the display, and retrieving top-level parent containers. It also includes optional Mac-specific UI hints (Harbor/JRS properties) for component styling.

The class is final and has a private constructor because it is purely a static utility.

  • Method Details

    • boundsOfMainScreen

      public static Rectangle boundsOfMainScreen()
      Returns the bounds of the "main" screen.

      The method tries the following logic in order:

      1. Return the bounds of the first screen whose x coordinate is 0. This is typically the primary monitor.
      2. If no such screen exists, return the bounds of the largest (by pixel area) display.
      3. If screen information is unavailable, fall back to the default Toolkit screen size.
      Returns:
      the rectangle representing the main screen's bounds; never null.
    • centerComponent

      public static void centerComponent(Component component)
      Centers the given component on the main screen.

      This is equivalent to calling centerComponent(Component, int, int) with zero offsets.

      Parameters:
      component - the component to center; ignored if null.
    • centerComponent

      public static void centerComponent(Component component, int dh, int dv)
      Centers a component on the main screen, optionally adjusting the final position by horizontal and vertical offsets.

      The method ensures that the component does not exceed the screen size, shrinking its calculated size if necessary, but it does not resize the component itself—only its placement rectangle.

      Parameters:
      component - the component to place; ignored if null.
      dh - horizontal offset from true center (positive → right).
      dv - vertical offset from true center (positive → down).
    • screenFraction

      public static Dimension screenFraction(double fraction)
      Returns a Dimension representing the given fraction of the total main screen size.

      For example, screenFraction(0.5) returns a dimension equal to half the screen width and half the screen height.

      Parameters:
      fraction - the fraction of the screen size (typically between 0 and 1).
      Returns:
      a dimension containing fraction * screenWidth and fraction * screenHeight.
    • getDisplaySize

      public static Dimension getDisplaySize()
      Returns the width and height of the main display as a Dimension.
      Returns:
      the display dimensions; never null.
    • sizeToScreen

      public static void sizeToScreen(JFrame frame, double fractionalSize)
      Sizes a JFrame to a fraction of the main screen and centers it.
      Parameters:
      frame - the frame to size and center.
      fractionalSize - the fraction (e.g., 0.8 for 80% of screen size).
    • getParentContainer

      public static Container getParentContainer(Component component)
      Returns the top-level Swing/AWT container associated with the given component.

      The search walks up the parent hierarchy until it finds one of:

      If no such ancestor exists, or if component is null, the method returns null.

      Parameters:
      component - the component whose top-level container should be found.
      Returns:
      the top-level container, or null if none exists.
    • setSizeSmall

      public static void setSizeSmall(JComponent component)
      Applies the macOS "small" size variant to a Swing component.
      Parameters:
      component - the component to modify.
    • setSizeMini

      public static void setSizeMini(JComponent component)
      Applies the macOS "mini" size variant to a Swing component.
      Parameters:
      component - the component to modify.
    • setSquareButton

      public static void setSquareButton(AbstractButton button)
      Marks the given button as a Mac-style square button, if supported by the installed Look and Feel.
      Parameters:
      button - the button to modify.
    • setTexturedButton

      public static void setTexturedButton(AbstractButton button)
      Marks the given button as a Mac-style textured button.
      Parameters:
      button - the button to modify.