Class JIcon

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, SwingConstants, StylableComponent

public class JIcon extends JLabel implements StylableComponent
A JLabel subclass specifically designed to display icons only. Although a JLabel already provides the ability to display icons, this class is useful for styling purposes, as it is possible to specifically target icons inside of StyleSheets.
See Also:
  • Constructor Details

    • JIcon

      public JIcon(String path)
      Constructs a JIcon with an icon loaded from the specified file path.
      Parameters:
      path - The file path to the icon resource.
    • JIcon

      public JIcon(IconDeclaration declaration)
      Constructs a JIcon with the specified icon declaration.
      Parameters:
      declaration - The icon declaration specifying which icon to display.
    • JIcon

      public JIcon(Icon icon)
      Constructs a JIcon with the specified icon.
      Parameters:
      icon - The icon to display.
    • JIcon

      public JIcon(Size size, Icon icon)
      Constructs a JIcon with the specified size and icon.
      Parameters:
      size - The desired size for the icon.
      icon - The icon to display.
    • JIcon

      public JIcon(Icon icon, String text, int horizontalAlignment)
      Constructs a JIcon with the specified icon, text, and horizontal alignment.
      Parameters:
      icon - The icon to display.
      text - The text to display alongside the icon.
      horizontalAlignment - The horizontal alignment of the icon and text.
    • JIcon

      public JIcon(String text, int horizontalAlignment)
      Constructs a JIcon with the specified text and horizontal alignment.
      Parameters:
      text - The text to display.
      horizontalAlignment - The horizontal alignment of the text.
    • JIcon

      public JIcon(String path, String text)
      Constructs a JIcon with the specified icon path and text.
      Parameters:
      path - The file path to the icon resource.
      text - The text to display alongside the icon.
    • JIcon

      public JIcon(sprouts.Val<IconDeclaration> declaration)
      Constructs a JIcon with a dynamically bound icon declaration. The icon will update whenever the declaration property changes.
      Parameters:
      declaration - A property holding the icon declaration to display.
    • JIcon

      public JIcon()
      Constructs an empty JIcon with no icon initially displayed.
  • Method Details

    • paintComponent

      public void paintComponent(Graphics g)
      Overrides:
      paintComponent in class JComponent
    • paintChildren

      public void paintChildren(Graphics g)
      This method is expected to be implemented as follows:
            @Override
            public void paintChildren(Graphics g){
                paintForeground(g, ()->super.paintChildren(g));
            }
        
      Specified by:
      paintChildren in interface StylableComponent
      Overrides:
      paintChildren in class JComponent
      Parameters:
      g - the graphics context to paint on, obtained from the component's JComponent::paintChildren(Graphics) method.
    • setUISilently

      public void setUISilently(ComponentUI ui)
      Description copied from interface: StylableComponent
      Certain style configurations require SwingTree to install a custom UI delegate. This method is used to set the UI delegate for the component but without triggering side effects like the former UI being uninstalled (which itself can cause a lot of undesired side effects).

      This method is not intended to be called by client code! It exists for internal use only and unfortunately cannot be protected or private due to the nature of the Swing API.

      The implementation of this method is expected to look like this:

        @Override
        public void setUISilently(ComponentUI ui){
            this.ui = ui; // no side effects
        }
        
      Specified by:
      setUISilently in interface StylableComponent
      Parameters:
      ui - the UI delegate to set for the component without triggering side effects.
    • updateUI

      public void updateUI()
      Overrides:
      updateUI in class JLabel