Class WindowPlacement
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 Summary
Modifier and TypeMethodDescriptionstatic RectangleReturns the bounds of the "main" screen.static voidcenterComponent(Component component) Centers the given component on the main screen.static voidcenterComponent(Component component, int dh, int dv) Centers a component on the main screen, optionally adjusting the final position by horizontal and vertical offsets.static DimensionReturns the width and height of the main display as aDimension.static ContainergetParentContainer(Component component) Returns the top-level Swing/AWT container associated with the given component.static DimensionscreenFraction(double fraction) Returns aDimensionrepresenting the given fraction of the total main screen size.static voidsetSizeMini(JComponent component) Applies the macOS "mini" size variant to a Swing component.static voidsetSizeSmall(JComponent component) Applies the macOS "small" size variant to a Swing component.static voidsetSquareButton(AbstractButton button) Marks the given button as a Mac-style square button, if supported by the installed Look and Feel.static voidsetTexturedButton(AbstractButton button) Marks the given button as a Mac-style textured button.static voidsizeToScreen(JFrame frame, double fractionalSize) Sizes aJFrameto a fraction of the main screen and centers it.
-
Method Details
-
boundsOfMainScreen
Returns the bounds of the "main" screen.The method tries the following logic in order:
- Return the bounds of the first screen whose
xcoordinate is0. This is typically the primary monitor. - If no such screen exists, return the bounds of the largest (by pixel area) display.
- If screen information is unavailable, fall back to the default Toolkit screen size.
- Returns:
- the rectangle representing the main screen's bounds; never
null.
- Return the bounds of the first screen whose
-
centerComponent
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 ifnull.
-
centerComponent
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 ifnull.dh- horizontal offset from true center (positive → right).dv- vertical offset from true center (positive → down).
-
screenFraction
Returns aDimensionrepresenting 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 * screenWidthandfraction * screenHeight.
-
getDisplaySize
Returns the width and height of the main display as aDimension.- Returns:
- the display dimensions; never
null.
-
sizeToScreen
Sizes aJFrameto a fraction of the main screen and centers it.- Parameters:
frame- the frame to size and center.fractionalSize- the fraction (e.g.,0.8for 80% of screen size).
-
getParentContainer
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
componentisnull, the method returnsnull.- Parameters:
component- the component whose top-level container should be found.- Returns:
- the top-level container, or
nullif none exists.
-
setSizeSmall
Applies the macOS "small" size variant to a Swing component.- Parameters:
component- the component to modify.
-
setSizeMini
Applies the macOS "mini" size variant to a Swing component.- Parameters:
component- the component to modify.
-
setSquareButton
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
Marks the given button as a Mac-style textured button.- Parameters:
button- the button to modify.
-