public class SwingHelper
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
(package private) static class |
SwingHelper.RowHeaderRenderer |
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_MIN_FONT_SIZE
The default value for a minimal font size when adjusting font sizes.
|
static int |
DEFAULT_NUM_ROWS
Default number of text rows which are expected to be displayed with good visibility.
|
static Dict<java.awt.RenderingHints.Key,java.lang.Object> |
DEFAULT_RENDER
A dictionary with rendering hints providing default settings for various properties.
|
static Dict<java.awt.RenderingHints.Key,java.lang.Object> |
FAST_RENDER
A dictionary with rendering hints providing fast settings for various properties.
|
static Dict<java.awt.RenderingHints.Key,java.lang.Object> |
NICE_RENDER
A dictionary with rendering hints providing nice settings for various properties.
|
static IntIndexable |
STANDARD_ICON_SIZES
Standard icon sizes under various operating systems.
|
Constructor and Description |
---|
SwingHelper() |
Modifier and Type | Method and Description |
---|---|
static void |
adjustFontSizes(int plusminus)
Adjust all font sizes used in the application.
|
static void |
adjustFontSizes(int plusminus,
java.awt.Component... updateRoots)
Adjust all font sizes used in the application.
|
static void |
adjustFontSizes(int plusminus,
java.awt.Component updateRoot)
Adjust all font sizes used in the application.
|
static void |
adjustFontSizes(int plusminus,
int allowedMinSize)
Adjust all font sizes used in the application.
|
static void |
adjustFontSizes(int plusminus,
int allowedMinSize,
java.awt.Component... updateRoots)
Adjust all font sizes used in the application.
|
static void |
adjustFontSizes(int plusminus,
int allowedMinSize,
java.awt.Component updateRoot)
Adjust all font sizes used in the application.
|
static javax.swing.JList<java.lang.String> |
buildRowHeader(javax.swing.JTable table,
java.util.Vector<java.lang.String> headers)
Build a row header view.
|
static javax.swing.JPanel |
createSimpleMultiColumnPanel(int numColumns,
Countable<? extends javax.swing.JComponent> components)
Create a panel to have a defined number of columns of automatic width.
|
static void |
enableHiDpiSupport(java.awt.Window mainWindow,
java.awt.Component... moreRoots)
Enable high DPI support for the given window.
|
static java.awt.geom.AffineTransform |
fitTransform(java.awt.geom.Rectangle2D source,
java.awt.geom.Rectangle2D target,
double margin)
Get a transformation which fits a source rectangle into a target rectangle with uniform
scaling.
|
static java.awt.geom.AffineTransform |
fitTransform(java.awt.geom.Rectangle2D source,
java.awt.geom.Rectangle2D target,
double margin,
boolean invertY)
Get a transformation which fits a source rectangle into a target rectangle with uniform
scaling.
|
static java.awt.geom.AffineTransform |
fitTransform(java.awt.geom.Rectangle2D source,
java.awt.geom.Rectangle2D target,
double margin,
boolean invertY,
boolean isotrop)
Get a transformation which fits a source rectangle into a target rectangle with uniform
scaling.
|
static int |
getAccumulatedFontSizeChanges()
Get the accumulated font size changes.
|
static java.awt.Rectangle |
getDrawableViewport(java.awt.Component comp)
Get the inner rectangle of a component which is available for drawing.
|
static java.awt.Rectangle |
getDrawableViewport(javax.swing.JComponent comp)
Get the inner rectangle of a Swing component which is available for drawing.
|
static int |
getMinUiFontSize()
Get the minimal size used in the UI fonts.
|
static int |
getModifiersExWithButton(java.awt.event.MouseEvent event)
Workaround for changed behavior of modifiers handling.
|
static java.awt.Rectangle |
getScreenViewport(java.awt.GraphicsConfiguration graphicsConfiguration)
Get the part of the screen available for user application windows.
|
static int |
getUsefulFontSize()
Get a useful font size for the default screen's default graphics configuration
in order to display
DEFAULT_NUM_ROWS text rows |
static int |
getUsefulFontSize(java.awt.GraphicsConfiguration config)
Get a useful font size for the given graphics configuration.
|
static int |
getUsefulFontSize(java.awt.GraphicsConfiguration config,
int nRows)
Get a useful font size for the given graphics configuration.
|
static int |
getUsefulFontSize(int nRows)
Get a useful font size for the given graphics configuration.
|
static void |
invokeMuchLater(int recursions,
java.lang.Runnable task)
Invoke a GUI related task much later.
|
static void |
invokeMuchLater(java.lang.Runnable task)
Invoke a GUI related task much later.
|
static javax.swing.Icon |
loadIconResource(java.lang.String resourcePath)
Load a potentially scalable icon.
|
static void |
prepareDefaultGraphics(java.awt.Graphics2D g2)
Prepare a graphics context to use default settings for rendering.
|
static void |
prepareFastGraphics(java.awt.Graphics2D g2)
Prepare a graphics context to use the fastest settings for rendering.
|
static void |
prepareGraphics(java.awt.Graphics2D g2,
Dict<java.awt.RenderingHints.Key,?> hints)
Prepare the rendering hints of a graphics context.
|
static void |
prepareNiceGraphics(java.awt.Graphics2D g2)
Prepare a graphics context to use high-quality rendering.
|
static int |
setupDefaultHiDpi()
Setup default high DPI settings.
|
static int |
setupDefaultHiDpi(java.awt.GraphicsConfiguration config)
Setup default high DPI settings.
|
static void |
setupSimpleMultiColumnPanel(javax.swing.JPanel panelWithGridBagLayout,
int numColumns,
Countable<? extends javax.swing.JComponent> components)
Set up a panel to have a defined number of columns of automatic width.
|
static void |
setupSimpleMultiColumnPanel(javax.swing.JPanel panelWithGridBagLayout,
int numColumns,
javax.swing.JComponent... components)
Set up a panel to have a defined number of columns of automatic width.
|
static void |
setupSimpleTwoColumnPanel(javax.swing.JPanel panelWithGridBagLayout,
java.lang.Iterable<Pair<javax.swing.JComponent>> rows)
Set up a panel to have two columns of automatic width.
|
static void |
showSimpleApplicationFrame(java.lang.String title,
javax.swing.JComponent component,
int width,
int height)
Show a frame with a central component.
|
static javax.swing.JFrame |
simpleApplicationFrame(java.lang.String title,
javax.swing.JComponent component,
int width,
int height)
Create a frame with a central component.
|
static javax.swing.JPanel |
simpleMultiColumnPanel(int numColumns,
javax.swing.JComponent... components)
Create a panel to have a defined number of columns of automatic width.
|
static javax.swing.JPanel |
simpleTwoColumnPanel(java.lang.Iterable<Pair<javax.swing.JComponent>> rows)
Set up a panel to have two columns of automatic width.
|
static javax.swing.JPanel |
simpleTwoColumnPanel(javax.swing.JComponent... components)
Set up a panel to have two columns of automatic width.
|
static javax.swing.JPanel |
simpleTwoColumnPanelFrom(Indexable<? extends javax.swing.JComponent> components)
Set up a panel to have two columns of automatic width from an indexable array of components.
|
static void |
updateUI(java.awt.Component component)
Calls the updateUI() method on swing components recursively.
|
static java.awt.geom.AffineTransform |
viewTransform(java.awt.geom.Rectangle2D source,
javax.swing.JComponent target,
double margin)
Get a transformation which fits a given source rectangle into a Swing component
using uniform scaling.
|
static java.awt.geom.AffineTransform |
viewTransform(java.awt.geom.Rectangle2D source,
javax.swing.JComponent target,
double margin,
boolean invertY)
Get a transformation which fits a given source rectangle into a Swing component
using uniform scaling.
|
static java.awt.geom.AffineTransform |
viewTransform(java.awt.geom.Rectangle2D source,
javax.swing.JComponent target,
double margin,
boolean invertY,
boolean isotrop)
Get a transformation which fits a given source rectangle into a Swing component
using uniform scaling.
|
public static final int DEFAULT_MIN_FONT_SIZE
public static final IntIndexable STANDARD_ICON_SIZES
public static final int DEFAULT_NUM_ROWS
public static final Dict<java.awt.RenderingHints.Key,java.lang.Object> DEFAULT_RENDER
RenderingHints.KEY_RENDERING
RenderingHints.KEY_ANTIALIASING
RenderingHints.KEY_FRACTIONALMETRICS
RenderingHints.KEY_TEXT_ANTIALIASING
RenderingHints.KEY_INTERPOLATION
RenderingHints.KEY_ALPHA_INTERPOLATION
RenderingHints.KEY_COLOR_RENDERING
public static final Dict<java.awt.RenderingHints.Key,java.lang.Object> NICE_RENDER
RenderingHints.KEY_RENDERING
RenderingHints.KEY_ANTIALIASING
RenderingHints.KEY_FRACTIONALMETRICS
RenderingHints.KEY_TEXT_ANTIALIASING
RenderingHints.KEY_INTERPOLATION
RenderingHints.KEY_ALPHA_INTERPOLATION
RenderingHints.KEY_COLOR_RENDERING
public static final Dict<java.awt.RenderingHints.Key,java.lang.Object> FAST_RENDER
RenderingHints.KEY_RENDERING
RenderingHints.KEY_ANTIALIASING
RenderingHints.KEY_FRACTIONALMETRICS
RenderingHints.KEY_TEXT_ANTIALIASING
RenderingHints.KEY_INTERPOLATION
RenderingHints.KEY_ALPHA_INTERPOLATION
RenderingHints.KEY_COLOR_RENDERING
public static void adjustFontSizes(int plusminus)
plusminus
- how to adjust, if positive fonts used become larger, if negative smallerpublic static void adjustFontSizes(int plusminus, int allowedMinSize)
plusminus
- how to adjust, if positive fonts used become larger, if negative smallerallowedMinSize
- the minimal font size result when shrinkingpublic static void adjustFontSizes(int plusminus, @Nullable java.awt.Component updateRoot)
plusminus
- how to adjust, if positive fonts used become larger, if negative smallerupdateRoot
- because for some reason the components don't update automatically,
here a root component may be added which will be explicitely updated using
the updateUI(java.awt.Component)
methodpublic static void adjustFontSizes(int plusminus, int allowedMinSize, @Nullable java.awt.Component updateRoot)
plusminus
- how to adjust, if positive fonts used become larger, if negative smallerallowedMinSize
- the minimal font size result when shrinkingupdateRoot
- because for some reason the components don't update automatically,
here a root component may be added which will be explicitly updated using
the updateUI(java.awt.Component)
methodpublic static void adjustFontSizes(int plusminus, java.awt.Component... updateRoots)
plusminus
- how to adjust, if positive fonts used become larger, if negative smallerupdateRoots
- because for some reason the components don't update automatically,
here root components may be added which will be explicitly updated using
the updateUI(java.awt.Component)
methodpublic static void adjustFontSizes(int plusminus, int allowedMinSize, java.awt.Component... updateRoots)
plusminus
- how to adjust, if positive fonts used become larger, if negative smallerallowedMinSize
- the minimal font size result when shrinkingupdateRoots
- because for some reason the components don't update automatically,
here root components may be added which will be explicitly updated using
the updateUI(java.awt.Component)
methodpublic static int getMinUiFontSize()
public static void updateUI(java.awt.Component component)
component
- root componentpublic static int getAccumulatedFontSizeChanges()
public static void enableHiDpiSupport(@NotNull java.awt.Window mainWindow, @NotNull java.awt.Component... moreRoots)
mainWindow
- application's main windowmoreRoots
- more root components for recursive enablementpublic static int setupDefaultHiDpi()
public static int setupDefaultHiDpi(@NotNull java.awt.GraphicsConfiguration config)
config
- graphics configuration used for setuppublic static int getUsefulFontSize()
DEFAULT_NUM_ROWS
text rowspublic static int getUsefulFontSize(int nRows)
nRows
- number of text rows expected to be visible on screenpublic static int getUsefulFontSize(@NotNull java.awt.GraphicsConfiguration config)
DEFAULT_NUM_ROWS
as number of visible rows.config
- graphics configurationpublic static int getUsefulFontSize(@NotNull java.awt.GraphicsConfiguration config, int nRows)
config
- graphics configurationnRows
- number of text rows expected to be visible on screen@Nullable public static javax.swing.Icon loadIconResource(@NotNull java.lang.String resourcePath)
resourcePath
- internal resource path to image@NotNull public static java.awt.Rectangle getDrawableViewport(@NotNull java.awt.Component comp)
comp
- component for which the draw rectangle is requested@NotNull public static java.awt.Rectangle getDrawableViewport(@NotNull javax.swing.JComponent comp)
comp
- component for which the draw rectangle is requested@NotNull public static java.awt.geom.AffineTransform fitTransform(@NotNull java.awt.geom.Rectangle2D source, @NotNull java.awt.geom.Rectangle2D target, double margin)
This is most useful for calculating view transformations for fitting a bounding rectangle into a screen rectangle.
source
- source rectangletarget
- target rectanglemargin
- margin subtracted from all borders of target rectangle, use 0.0
for no marginviewTransform(Rectangle2D, JComponent, double)
@NotNull public static java.awt.geom.AffineTransform fitTransform(@NotNull java.awt.geom.Rectangle2D source, @NotNull java.awt.geom.Rectangle2D target, double margin, boolean invertY)
This is most useful for calculating view transformations for fitting a bounding rectangle into a screen rectangle.
source
- source rectangletarget
- target rectanglemargin
- margin subtracted from all borders of target rectangle, use 0.0
for no margininvertY
- invert the Y axis?viewTransform(Rectangle2D, JComponent, double)
@NotNull public static java.awt.geom.AffineTransform fitTransform(@NotNull java.awt.geom.Rectangle2D source, @NotNull java.awt.geom.Rectangle2D target, double margin, boolean invertY, boolean isotrop)
This is most useful for calculating view transformations for fitting a bounding rectangle into a screen rectangle.
source
- source rectangletarget
- target rectanglemargin
- margin subtracted from all borders of target rectangle, use 0.0
for no marginisotrop
- scale the same in both directions?invertY
- invert the Y axis?viewTransform(Rectangle2D, JComponent, double)
@NotNull public static java.awt.geom.AffineTransform viewTransform(@NotNull java.awt.geom.Rectangle2D source, @NotNull javax.swing.JComponent target, double margin)
This especially takes care of possible borders applied to target
.
source
- source rectangletarget
- target rectanglemargin
- margin subtracted from all borders of target rectangle, use 0.0
for no marginviewTransform(Rectangle2D, JComponent, double)
,
getDrawableViewport(JComponent)
@NotNull public static java.awt.geom.AffineTransform viewTransform(@NotNull java.awt.geom.Rectangle2D source, @NotNull javax.swing.JComponent target, double margin, boolean invertY)
This especially takes care of possible borders applied to target
.
source
- source rectangletarget
- target rectanglemargin
- margin subtracted from all borders of target rectangle, use 0.0
for no margininvertY
- invert the Y axis?viewTransform(Rectangle2D, JComponent, double)
,
getDrawableViewport(JComponent)
@NotNull public static java.awt.geom.AffineTransform viewTransform(@NotNull java.awt.geom.Rectangle2D source, @NotNull javax.swing.JComponent target, double margin, boolean invertY, boolean isotrop)
This especially takes care of possible borders applied to target
.
source
- source rectangletarget
- target rectanglemargin
- margin subtracted from all borders of target rectangle, use 0.0
for no margininvertY
- invert the Y axis?isotrop
- scale the same in both directions?viewTransform(Rectangle2D, JComponent, double)
,
getDrawableViewport(JComponent)
public static int getModifiersExWithButton(@NotNull java.awt.event.MouseEvent event)
InputEvent.getModifiers()
contains the relevant
buttons even in the release case, while MouseEvent.getModifiersEx()
does not. This method includes the button in the modifiers
in its down state even although this is is basically wrong.
This allows old logic to still do its work.event
- mouse eventMouseEvent.getModifiersEx()
with
the down flag for the button of interestpublic static void invokeMuchLater(@NotNull java.lang.Runnable task)
8
.task
- task to perform much laterinvokeMuchLater(int, Runnable)
public static void invokeMuchLater(int recursions, @NotNull java.lang.Runnable task)
SwingUtilities.invokeLater(Runnable)
until the
task is performed. Using 1
for recursions
will make this method behave likerecursions
- number of reinvocations until task
is performedtask
- task to perform much later@NotNull public static java.awt.Rectangle getScreenViewport(@NotNull java.awt.GraphicsConfiguration graphicsConfiguration)
graphicsConfiguration
- graphics configuration of the screen of interestpublic static void prepareGraphics(@NotNull java.awt.Graphics2D g2, @NotNull Dict<java.awt.RenderingHints.Key,?> hints)
hints
for the given graphics.g2
- graphics contexthints
- rendering hints to setpublic static void prepareDefaultGraphics(@NotNull java.awt.Graphics2D g2)
g2
- graphics contextprepareGraphics(Graphics2D, Dict)
,
prepareNiceGraphics(Graphics2D)
,
prepareFastGraphics(Graphics2D)
public static void prepareNiceGraphics(@NotNull java.awt.Graphics2D g2)
g2
- graphics contextprepareGraphics(Graphics2D, Dict)
,
prepareDefaultGraphics(Graphics2D)
,
prepareFastGraphics(Graphics2D)
public static void prepareFastGraphics(@NotNull java.awt.Graphics2D g2)
g2
- graphics contextprepareGraphics(Graphics2D, Dict)
,
prepareDefaultGraphics(Graphics2D)
,
prepareNiceGraphics(Graphics2D)
@NotNull public static javax.swing.JPanel simpleTwoColumnPanel(@NotNull javax.swing.JComponent... components)
This is useful for displaying name-value pairs in cases where a table is not sufficient.
components
- components ordered left, right, left, right, ...@NotNull public static javax.swing.JPanel simpleTwoColumnPanelFrom(@NotNull Indexable<? extends javax.swing.JComponent> components)
This is useful for displaying name-value pairs in cases where a table is not sufficient.
components
- components ordered left, right, left, right, ...@NotNull public static javax.swing.JPanel simpleTwoColumnPanel(@NotNull java.lang.Iterable<Pair<javax.swing.JComponent>> rows)
This is useful for displaying name-value pairs in cases where a table is not sufficient.
rows
- pairs of components for the rows of the panelpublic static void setupSimpleTwoColumnPanel(@NotNull javax.swing.JPanel panelWithGridBagLayout, @NotNull java.lang.Iterable<Pair<javax.swing.JComponent>> rows)
This is useful for displaying name-value pairs in cases where a table is not sufficient.
panelWithGridBagLayout
- panel, required to have a GridBagLayout
rows
- pairs of components for the rows of the panel@NotNull public static javax.swing.JPanel simpleMultiColumnPanel(int numColumns, @NotNull javax.swing.JComponent... components)
numColumns
- number of columnscomponents
- components to add, the number has to be a multiple of numColums
,
use null
to keep a cell emptypublic static void setupSimpleMultiColumnPanel(@NotNull javax.swing.JPanel panelWithGridBagLayout, int numColumns, @NotNull javax.swing.JComponent... components)
panelWithGridBagLayout
- panel, required to have a GridBagLayout
numColumns
- number of columnscomponents
- components to add, the number has to be a multiple of numColums
,
use null
to keep a cell empty@NotNull public static javax.swing.JPanel createSimpleMultiColumnPanel(int numColumns, @NotNull Countable<? extends javax.swing.JComponent> components)
numColumns
- number of columns, at least 1
components
- components to add, the number has to be a multiple of numColums
,
use null
to keep a cell emptypublic static void setupSimpleMultiColumnPanel(@NotNull javax.swing.JPanel panelWithGridBagLayout, int numColumns, @NotNull Countable<? extends javax.swing.JComponent> components)
panelWithGridBagLayout
- panel, required to have a GridBagLayout
numColumns
- number of columns, at least 1
components
- components to add, the number has to be a multiple of numColums
,
use null
to keep a cell empty@NotNull public static javax.swing.JFrame simpleApplicationFrame(@NotNull java.lang.String title, @NotNull javax.swing.JComponent component, int width, int height)
title
- frame titlecomponent
- central componentwidth
- frame width (use 0
for packed)height
- frame height (use 0
for packedpublic static void showSimpleApplicationFrame(@NotNull java.lang.String title, @NotNull javax.swing.JComponent component, int width, int height)
title
- frame titlecomponent
- central componentwidth
- frame width (use 0
for packed)height
- frame height (use 0
for packed@NotNull public static javax.swing.JList<java.lang.String> buildRowHeader(@NotNull javax.swing.JTable table, @NotNull java.util.Vector<java.lang.String> headers)
table
- table for which the row header is createdheaders
- headers to set, should have the same number of rows as tablerow header view component