Class IconBorder

  • All Implemented Interfaces:
    java.io.Serializable, javax.swing.border.Border

    public class IconBorder
    extends javax.swing.border.AbstractBorder
    Paints an icon on the left or right side of a component, depending on reading direction. Intended to be used to add decorative icons to text fields.
    Since:
    3.0
    Author:
    Chris Jennings
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      IconBorder()  
      IconBorder​(java.net.URL icon)  
      IconBorder​(java.net.URL icon, boolean showOnRightSide)  
      IconBorder​(javax.swing.Icon icon)  
      IconBorder​(javax.swing.Icon icon, boolean showOnRightSide)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static IconBorder applyLabelBorder​(javax.swing.JComponent target, java.lang.String prefix, javax.swing.Icon icon, javax.swing.Icon hoverIcon, java.awt.event.ActionListener li, javax.swing.JPopupMenu menu)  
      java.awt.Insets getBorderInsets​(java.awt.Component c)  
      java.awt.Insets getBorderInsets​(java.awt.Component c, java.awt.Insets insets)  
      javax.swing.Icon getIcon()  
      int getInnerIconMargin()  
      int getOuterIconMargin()  
      javax.swing.border.Border install​(javax.swing.JComponent c)
      Installs this border on a component by setting a new compound border on the component with the existing border on the outside and this border on the inside.
      javax.swing.border.Border installClickable​(javax.swing.JComponent c, javax.swing.Icon hoverIcon, java.awt.event.ActionListener li, javax.swing.JPopupMenu popupMenu)
      Installs this border on a component and makes the icon border clickable.
      boolean isBorderOpaque()  
      boolean isIconOnRightSide()  
      boolean isPointOverIcon​(javax.swing.JComponent c, int x, int y)
      Assuming that a component has this border installed on it, returns true if a point in the component's coordinate space would be over the icon.
      void paintBorder​(java.awt.Component c, java.awt.Graphics g, int x, int y, int width, int height)  
      void setIcon​(javax.swing.Icon icon)  
      void setIconOnRightSide​(boolean rightSide)  
      void setInnerIconMargin​(int gap)  
      void setOuterIconMargin​(int gap)  
      • Methods inherited from class javax.swing.border.AbstractBorder

        getBaseline, getBaselineResizeBehavior, getInteriorRectangle, getInteriorRectangle
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • IconBorder

        public IconBorder()
      • IconBorder

        public IconBorder​(javax.swing.Icon icon)
      • IconBorder

        public IconBorder​(java.net.URL icon)
      • IconBorder

        public IconBorder​(javax.swing.Icon icon,
                          boolean showOnRightSide)
      • IconBorder

        public IconBorder​(java.net.URL icon,
                          boolean showOnRightSide)
    • Method Detail

      • getIcon

        public javax.swing.Icon getIcon()
      • setIcon

        public void setIcon​(javax.swing.Icon icon)
      • isIconOnRightSide

        public boolean isIconOnRightSide()
      • setIconOnRightSide

        public void setIconOnRightSide​(boolean rightSide)
      • isPointOverIcon

        public boolean isPointOverIcon​(javax.swing.JComponent c,
                                       int x,
                                       int y)
        Assuming that a component has this border installed on it, returns true if a point in the component's coordinate space would be over the icon.
        Parameters:
        c - the component to test
        x - the x-coordinate of the point
        y - the y-coordinate of the point
        Returns:
        true if (x,y) is over the icon border
      • install

        public javax.swing.border.Border install​(javax.swing.JComponent c)
        Installs this border on a component by setting a new compound border on the component with the existing border on the outside and this border on the inside. Returns the new compound border.

        If the component has no border, this border is simply set on the component without creating a compound border.

        If the component is an editable combo box, the icon will be installed on the editor component if it is a JComponent.

        Parameters:
        c - the component to install the border on, typically a text field
        Returns:
        the installed compound border
      • installClickable

        public javax.swing.border.Border installClickable​(javax.swing.JComponent c,
                                                          javax.swing.Icon hoverIcon,
                                                          java.awt.event.ActionListener li,
                                                          javax.swing.JPopupMenu popupMenu)
        Installs this border on a component and makes the icon border clickable. This behaves exactly as for calling install(javax.swing.JComponent), except that the border also responds to mouse events.
        Parameters:
        c - the component to install in
        hoverIcon - an optional alternate icon displayed when the pointer is over the icon
        li - an optional listener that will be called when the icon is left clicked
        popupMenu - an optional popup menu that will be displayed when the icon is right clicked
        Returns:
        the installed compound border
      • getBorderInsets

        public java.awt.Insets getBorderInsets​(java.awt.Component c)
        Specified by:
        getBorderInsets in interface javax.swing.border.Border
        Overrides:
        getBorderInsets in class javax.swing.border.AbstractBorder
      • getBorderInsets

        public java.awt.Insets getBorderInsets​(java.awt.Component c,
                                               java.awt.Insets insets)
        Overrides:
        getBorderInsets in class javax.swing.border.AbstractBorder
      • isBorderOpaque

        public boolean isBorderOpaque()
        Specified by:
        isBorderOpaque in interface javax.swing.border.Border
        Overrides:
        isBorderOpaque in class javax.swing.border.AbstractBorder
      • paintBorder

        public void paintBorder​(java.awt.Component c,
                                java.awt.Graphics g,
                                int x,
                                int y,
                                int width,
                                int height)
        Specified by:
        paintBorder in interface javax.swing.border.Border
        Overrides:
        paintBorder in class javax.swing.border.AbstractBorder
      • setOuterIconMargin

        public void setOuterIconMargin​(int gap)
      • getOuterIconMargin

        public int getOuterIconMargin()
      • setInnerIconMargin

        public void setInnerIconMargin​(int gap)
      • getInnerIconMargin

        public int getInnerIconMargin()
      • applyLabelBorder

        public static IconBorder applyLabelBorder​(javax.swing.JComponent target,
                                                  java.lang.String prefix,
                                                  javax.swing.Icon icon,
                                                  javax.swing.Icon hoverIcon,
                                                  java.awt.event.ActionListener li,
                                                  javax.swing.JPopupMenu menu)