Interface IconDeclaration
Enum type
that declares a set of icon paths so that the enum instances
can be used to identify and load
(cached) icons across your application.
Here an example of how to use this interface:
public enum Icons implements IconDeclaration
{
ADD("icons/add.png"),
REMOVE("icons/remove.png"),
EDIT("icons/edit.png"),
SAVE("icons/save.png"),
CANCEL("icons/cancel.png"),
REFRESH("icons/refresh.png");
// ...
private final String path;
Icons(String path) { this.path = path; }
{@literal @}Override public String source() {
return path;
}
}
You may then use the enum instances
in the SwingTree API just like you would use the ImageIcon class:
UI.button(Icons.ADD)
.onClick( it -> vm.add() )
The reason why enums should be used instead of Strings is
so that you have some more compile time safety in your application!
When it comes to resource loading Strings are brittle because they
are susceptible to typos and refactoring mistakes.
Instances of this class are intended to be used as part of a view model
instead of using the Icon or ImageIcon classes directly.
The reason for this is the fact that traditional Swing icons
are often heavyweight objects whose loading may or may not succeed, and so they are
not suitable for direct use in a property as part of your view-model.
Instead, you should use this IconDeclaration interface, which is a
lightweight value object that merely models the resource location of the icon
even if it is not yet loaded or even does not exist at all.
Not only does this make your view model more robust, but it also allows you to write unit tests much more easily. You can create icon declarations even if the targeted icon may not be available at all, yet you can still test the behavior of your view-model.
SVG Support:
In addition to traditional image files, IconDeclaration supports
programmatically defined SVG icons through the ofAutoScaledSvg(String) factory method.
This enables creation of resolution-independent vector icons directly in code:
// Create a custom SVG icon declaration
IconDeclaration playButton = IconDeclaration.ofSvg(
"<svg width='24' height='24' viewBox='0 0 24 24'>" +
" <path d='M8 5v14l11-7z' fill='currentColor'/>" +
"</svg>"
);
// Use it in UI declarations
UI.button(playButton)
.onClick(it -> mediaPlayer.play())
SVG icons automatically scale to match DPI settings and maintain crisp edges
at any resolution. They can be sized dynamically or set to a fixed size
using withSize(Size).-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumDefines the format of the source string returned bysource(). -
Method Summary
Modifier and TypeMethodDescriptionfind()static IconDeclarationA factory method for creating anIconDeclarationinstance from the provided path to the icon resource.static IconDeclarationA factory method for creating anIconDeclarationinstance from the provided path to the icon resource and the preferred size.static IconDeclarationof(Size size, IconDeclaration.SourceFormat sourceFormat, String source) Creates anIconDeclarationinstance with full control over all declaration properties: size, source format, and source content.static IconDeclarationofAutoScaledSvg(String svg) Creates anIconDeclarationinstance from an SVG document string, which will be resolved to anSvgIconwith an unknown size, effectively making it scale depending on its usage context. This factory method is specifically designed for creating vector-based icons programmatically without requiring external files.static IconDeclarationCreates anIconDeclarationinstance from an SVG document string, which will be resolved to anSvgIconwith the same size declared in the SVG text. This factory method is specifically designed for creating vector-based icons programmatically without requiring external files.size()The preferred size of the icon, which is not necessarily the actual size of the icon that is being loaded but rather the size that the icon should be scaled to when it is being loaded.
If this method returnsOptional.empty(), then the loaded icon will have the exact same size found at the image file found atsource().source()This method supplies a String which is a "source" for producingImageIcon, this is typically a path to a file, but may also be a full-blown SVG document in text form.
The exact meaning of the source String is defined by theIconDeclaration.SourceFormatreturned bysourceFormat()method.default IconDeclaration.SourceFormatReturns the format of the source string returned bysource().default IconDeclarationwithHeight(int height) Allows you to create an updatedIconDeclarationinstance with a new preferred height for the icon.default IconDeclarationwithSize(int width, int height) Creates and returns an updatedIconDeclarationinstance with a new preferred width and height for the icon.default IconDeclarationCreates and returns an updatedIconDeclarationinstance with a new preferred size for the icon.default IconDeclarationwithWidth(int width) Creates and returns an updatedIconDeclarationinstance with a new preferred width for the icon.
-
Method Details
-
source
String source()This method supplies a String which is a "source" for producingImageIcon, this is typically a path to a file, but may also be a full-blown SVG document in text form.
The exact meaning of the source String is defined by theIconDeclaration.SourceFormatreturned bysourceFormat()method. Together, these two properties form the most important part an icon declaration, since they constitute the minimum amount of information needed to resolve an actual icon...
Note that in case of the source String being aIconDeclaration.SourceFormat.PATH_TO_ICON, it may either be a path relative to the classpath or may be an absolute path.- Returns:
- The "source String" which is used together with the
sourceFormat()to resolve an icon. It is typically a path to the icon resource, which is relative to the classpath or may be an absolute path.
-
sourceFormat
Returns the format of the source string returned bysource(). This determines how the source string should be interpreted when loading or creating the icon.By default, this method returns
IconDeclaration.SourceFormat.PATH_TO_ICON, meaning the source string is treated as a file path. Implementations can override this method to returnIconDeclaration.SourceFormat.SVG_STRINGif they provide SVG content directly.- Returns:
- The format of the source string, never
null.
-
size
The preferred size of the icon, which is not necessarily the actual size of the icon that is being loaded but rather the size that the icon should be scaled to when it is being loaded.
If this method returnsOptional.empty(), then the loaded icon will have the exact same size found at the image file found atsource(). A non-empty optional with aSize.unknown(), specifically indicates toSvgIcons that their size should be context dependent, meaning that theirSvgIcon.getIconWidth()andSvgIcon.getIconHeight()are both returning-1to indicate flexible scaling.
In case of a regular png or jpeg on the other hand, aSize.unknown()will also cause the resultingImageIconto have the same dimensions found in the file.- Returns:
- The preferred size of the icon or
Optional.empty()if the size is unspecified, which means that the icon should be loaded in its original size. A non-empty optional with aSize.unknown(), specifically indicates toSvgIcons that their size should be context dependent.
-
find
-
withSize
Creates and returns an updatedIconDeclarationinstance with a new preferred size for the icon.- Parameters:
size- The preferred size of the icon in the form of aSizeinstance.- Returns:
- A new
IconDeclarationinstance with the same path but with the given size.
-
withSize
Creates and returns an updatedIconDeclarationinstance with a new preferred width and height for the icon.- Parameters:
width- The preferred width of the icon.height- The preferred height of the icon.- Returns:
- A new
IconDeclarationinstance with the same path but with the specified width and height as preferred size.
-
withWidth
Creates and returns an updatedIconDeclarationinstance with a new preferred width for the icon.- Parameters:
width- The preferred width of the icon.- Returns:
- A new
IconDeclarationinstance with the same path but with the specified width as preferred width.
-
withHeight
Allows you to create an updatedIconDeclarationinstance with a new preferred height for the icon.- Parameters:
height- The preferred height of the icon.- Returns:
- A new
IconDeclarationinstance with the same path but with the specified height as preferred height.
-
find
-
of
A factory method for creating anIconDeclarationinstance from the provided path to the icon resource.- Parameters:
path- The path to the icon resource, which may be relative to the classpath or may be an absolute path.- Returns:
- A new
IconDeclarationinstance that represents the given icon resource as a constant.
-
of
A factory method for creating anIconDeclarationinstance from the provided path to the icon resource and the preferred size.- Parameters:
size- The preferred size of the icon.path- The path to the icon resource, which may be relative to the classpath or may be an absolute path.- Returns:
- A new
IconDeclarationinstance that represents the given icon resource as a constant.
-
ofAutoScaledSvg
Creates anIconDeclarationinstance from an SVG document string, which will be resolved to anSvgIconwith an unknown size, effectively making it scale depending on its usage context. This factory method is specifically designed for creating vector-based icons programmatically without requiring external files.The provided SVG string must be a complete, well-formed SVG XML document. The resulting icon declaration will have its
sourceFormat()set toIconDeclaration.SourceFormat.SVG_STRING.Icon declarations produced by this method will resolve to
SvgIconinstances, which support dynamic scaling while maintaining crisp edges at any scale.Example:
In the above example, anString checkmarkSvg = "<svg width='16' height='16'>" + "<path d='M2 8l4 4l8-8' stroke='green' stroke-width='2' fill='none'/>" + "</svg>"; IconDeclaration checkmark = IconDeclaration.ofAutoScaledSvg(checkmarkSvg) .withSize(24, 24);SvgIconloaded using this declaration will report-1for bothSvgIcon.getIconWidth()andSvgIcon.getIconHeight()instead of 16! This is to ensure that the svg is scalable when used as component icons...- Parameters:
svg- The complete SVG document as a string. Must not benull.- Returns:
- A new
IconDeclarationinstance representing the SVG icon. - Throws:
NullPointerException- ifsvgisnull.
-
ofSvg
Creates anIconDeclarationinstance from an SVG document string, which will be resolved to anSvgIconwith the same size declared in the SVG text. This factory method is specifically designed for creating vector-based icons programmatically without requiring external files.The provided SVG string must be a complete, well-formed SVG XML document. The resulting icon declaration will have its
sourceFormat()set toIconDeclaration.SourceFormat.SVG_STRING.Icon declarations produced by this method will resolve to
SvgIconinstances, which support dynamic scaling while maintaining crisp edges at any scale.Example:
In the above example, anString checkmarkSvg = "<svg width='16' height='16'>" + "<path d='M2 8l4 4l8-8' stroke='green' stroke-width='2' fill='none'/>" + "</svg>"; IconDeclaration checkmark = IconDeclaration.ofSvg(checkmarkSvg) .withSize(24, 24);SvgIconloaded using this declaration will report16for bothSvgIcon.getIconWidth()andSvgIcon.getIconHeight().- Parameters:
svg- The complete SVG document as a string. Must not benull.- Returns:
- A new
IconDeclarationinstance representing the SVG icon. - Throws:
NullPointerException- ifsvgisnull.
-
of
Creates anIconDeclarationinstance with full control over all declaration properties: size, source format, and source content.This is the most comprehensive factory method, allowing precise specification of how the icon should be interpreted and displayed. It's particularly useful when creating icon declarations programmatically or when you need explicit control over the source format.
Size Handling: If the provided
sizeparameter isSize.unknown(), it will indicate to raster based icons that they should use their natural dimensions (for raster images), whereas SVG based icons will be loaded without any size to make their size effectively context-dependent (automatic scaling).Example Usage:
// Create a PNG icon declaration with specific size IconDeclaration icon1 = IconDeclaration.of( Size.of(32, 32), SourceFormat.PATH_TO_ICON, "icons/user.png" ); // Create an SVG icon declaration with unknown (flexible) size IconDeclaration icon2 = IconDeclaration.of( Size.unknown(), SourceFormat.SVG_STRING, "<svg ...>...</svg>" );- Parameters:
size- The preferred display size for the icon. UseSize.unknown()to indicate natural/context-dependent sizing. Must not benull.sourceFormat- The format interpretation of the source string. Must not benull.source- The icon source content (file path or SVG XML). Must not benull.- Returns:
- A new
IconDeclarationinstance configured with the specified properties. - Throws:
NullPointerException- if any parameter isnull.- See Also:
-