Package swingtree

Class UI.Font

java.lang.Object
swingtree.UI.Font
Enclosing class:
UI

public static final class UI.Font extends Object
An immutable and value based font configuration object, which is used to configure component fonts in a UI declaration. SwingTree can dynamically generate Fonts for components using the toAwtFont() method. This may be done reactively, like for example when the look and feel changes its UI scale and SwingTree scales and re-installs your fonts.
This is a wrapper around a FontConf object, which is also used in the UIForAnySwing.withStyle(Styler) API, and it holds all font properties needed to create Font instances for Swing components. The appearance of a font is primarily based on the font family name which is used to find a font on the system, but there is a variety of other properties which you can configure using with(Configurator).
We recommend using this class instead of classical AWT Font instances in your code, as it allows SwingTree to scale your UI more reliably.
See Also:
  • Field Details

    • UNDEFINED

      public static final Font UNDEFINED
      This constant is a Font object with a font name of "" (empty string), a font style of -1 (undefined) and a font size of 0. Its identity is used to represent the absence of a font being specified, and it is used as a safe replacement for null, meaning that when the style engine of a component encounters it, it will pass it onto the Component.setFont(java.awt.Font) method as null. Passing null to this method means that the look and feel determines the font.
  • Method Details

    • of

      public static UI.Font of(String name, UI.FontStyle style, int size)
      A factory method that creates a new Font object with the specified font name UI.FontStyle and size. This maps directly to the constructor of Font(String, int, int).
      Parameters:
      name - The font name, which may be anything depending on what fonts are loaded on the system.
      style - The style of the font, which is one of the constants UI.FontStyle.PLAIN,
      size - The point size of the font.
      Returns:
      A new Font object with the specified font name, style and size.
    • of

      public static UI.Font of(String name, int size)
      A factory method that creates a new Font object with the specified font name and size where the UI.FontStyle defaults to PLAIN. This maps directly to the constructor of Font(String, int, int).
      Parameters:
      name - The font name, which may be anything depending on what fonts are loaded on the system.
      size - The point size of the font.
      Returns:
      A new Font object with the specified font name, style and size.
    • of

      public static UI.Font of(Map<? extends AttributedCharacterIterator.Attribute,?> attributes)
      Creates a new Font object from a map of attributes where the key is an attribute and the value is the value of the attribute. See TextAttribute for a list of common attributes. These attributes define the style of the font.
      Parameters:
      attributes - A map of attributes that define the style of the font.
      Returns:
      A new Font object with the specified attributes.
    • of

      public static UI.Font of(Font font)
      Creates a new UI.Font object from a Font object.
      Parameters:
      font - The font to convert to a font.
      Returns:
      The SwingTree native font object.
    • toAwtFont

      public Font toAwtFont()
      Converts this font to a Font object that can be used in Swing components. Note that this method creates a new Font object which will always be scaled according to the current DPI settings of SwingTree, which you can access using UI.scale().
      Returns:
      The Font object representing this font.
    • conf

      public FontConf conf()
      Returns the configuration of this font which encapsulates all properties of the font and can be used to convert to a Font object using the FontConf.toAwtFont() method.
      Returns:
      The configuration of this font.
    • with

      public UI.Font with(Configurator<FontConf> configurator)
      Allows configuring this font using a configurator function to a new Font object with the desired changes.
      Parameters:
      configurator - The configurator function that applies changes to the font configuration. It receives a FontConf object representing the current configuration of the font and should return the modified FontConf object.
      Returns:
      A new Font object with the applied changes.
      Throws:
      NullPointerException - If the configurator is null.
    • withName

      public UI.Font withName(String name)
      Returns an updated version of this font with the font (family) name changed to the specified value.
      Parameters:
      name - The font name, which may be anything depending on what fonts are loaded on the system.
      Returns:
      A new Font object with the font name changed.
      Throws:
      NullPointerException - If the font name is null.
    • withStyle

      public UI.Font withStyle(UI.FontStyle style)
      Returns an updated version of this font with the font style changed to the specified value.
      Parameters:
      style - The style of the font, which is one of the constants UI.FontStyle.PLAIN, UI.FontStyle.BOLD or UI.FontStyle.ITALIC.
      Returns:
      A new Font object with the font style changed.
    • withSize

      public UI.Font withSize(int size)
      Returns an updated version of this font with the font size changed to the specified value.
      Parameters:
      size - The point size of the font.
      Returns:
      A new Font object with the font size changed.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object