Class MultiSelectItem

  • All Implemented Interfaces:
    java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible, javax.swing.SwingConstants

    public class MultiSelectItem
    extends javax.swing.JLabel
    This component draws an outlined text string. The component can include a delete button inside the outline. The addDeleteListener(Consumer) method can be used to handle clicks on the delete button.

    If this component is set to opaque then the outline will be filled with the background color.

    This component is used by the MultiSelectField to draw selected items.

    The following resources are used to configure the apearance of this component and can be overridden by a custom resource bundle.

    • multiSelectItem.background - the background color used to fill the outline
    • multiSelectItem.selectedBackground - the background color used to fill the outline when selected is true
    • multiSelectItem.outline.color - the color of the outline
    • multiSelectItem.outline.strokeWidth - the stroke width of the outline
    • multiSelectItem.outline.flatness - length of the sides to be drawn as a straight line (0 for semicircle sides)
    • multiSelectItem.outline.roundness.button - roundness of the outline when showDelete is true
    • multiSelectItem.outline.roundness.noButton - roundness of the outline when showDelete is false
    • multiSelectItem.button.color - color of the delete button
    • multiSelectItem.button.hoverColor - highlight color for the delete button when the mouse cursor is over it
    • multiSelectItem.button.size - radius of the delete button
    See Also:
    ComponentResources, MultiSelectField, Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected static class  MultiSelectItem.ButtonGeometry  
      • Nested classes/interfaces inherited from class javax.swing.JLabel

        javax.swing.JLabel.AccessibleJLabel
      • Nested classes/interfaces inherited from class javax.swing.JComponent

        javax.swing.JComponent.AccessibleJComponent
      • Nested classes/interfaces inherited from class java.awt.Container

        java.awt.Container.AccessibleAWTContainer
      • Nested classes/interfaces inherited from class java.awt.Component

        java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static java.awt.Color background  
      protected static java.awt.Color buttonColor  
      protected MultiSelectItem.ButtonGeometry buttonGeometry  
      protected static java.awt.Color buttonHoverColor  
      protected static float buttonRoundness  
      protected static int buttonSize  
      protected static int buttonSizeSquared  
      protected static double CROSS_SIZE  
      static int GAP  
      protected static float noButtonRoundness  
      protected static java.awt.Color outlineColor  
      protected float outlineRoundness  
      protected static java.awt.Color selectedBackground  
      protected static int strokeWidth  
      • Fields inherited from class javax.swing.JLabel

        labelFor
      • Fields inherited from class javax.swing.JComponent

        listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
      • Fields inherited from class java.awt.Component

        accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
      • Fields inherited from interface java.awt.image.ImageObserver

        ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
      • Fields inherited from interface javax.swing.SwingConstants

        BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
    • Constructor Summary

      Constructors 
      Constructor Description
      MultiSelectItem​(java.lang.String text, boolean showDelete, boolean fill)
      Create a MultiSelectItem.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addDeleteListener​(java.util.function.Consumer<MultiSelectItem> listener)
      Add a listener to be notified when the delete button is clicked.
      protected void drawDeleteButton​(java.awt.Graphics2D g2d, boolean hoverEffect)
      Draw the delete button.
      protected void fireDelete()
      Notifies listeners that the delete button has been clicked.
      java.awt.Insets getInsets()
      Overridden to add space for the outline and delete button.
      java.awt.Insets getInsets​(java.awt.Insets insets)
      Overridden to add space for the outline and delete button.
      int getMinHeight()  
      protected java.awt.Shape getOutline​(int x, int y, int width, int height)
      Get the outline to draw around the text.
      boolean isFill()  
      boolean isSelected()  
      boolean isShowDelete()
      Get the flag that indicates whether to draw the delete button.
      protected void onMouseMove​(java.awt.Point point)
      Updates the hover state of the delete button.
      protected void paintComponent​(java.awt.Graphics g)
      Overridden to draw the outline and delete button.
      void removeDeleteListener​(java.util.function.Consumer<MultiSelectItem> listener)
      Remove a delete button listener.
      void setBackgroundSupplier​(java.util.function.Function<MultiSelectItem,​java.awt.Color> backgroundSupplier)  
      void setFill​(boolean fill)  
      void setSelected​(boolean selected)  
      • Methods inherited from class javax.swing.JLabel

        checkHorizontalKey, checkVerticalKey, getAccessibleContext, getDisabledIcon, getDisplayedMnemonic, getDisplayedMnemonicIndex, getHorizontalAlignment, getHorizontalTextPosition, getIcon, getIconTextGap, getLabelFor, getText, getUI, getUIClassID, getVerticalAlignment, getVerticalTextPosition, imageUpdate, paramString, setDisabledIcon, setDisplayedMnemonic, setDisplayedMnemonic, setDisplayedMnemonicIndex, setHorizontalAlignment, setHorizontalTextPosition, setIcon, setIconTextGap, setLabelFor, setText, setUI, setVerticalAlignment, setVerticalTextPosition, updateUI
      • Methods inherited from class javax.swing.JComponent

        addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
      • Methods inherited from class java.awt.Container

        add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
      • Methods inherited from class java.awt.Component

        action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • strokeWidth

        protected static final int strokeWidth
      • outlineColor

        protected static final java.awt.Color outlineColor
      • buttonColor

        protected static final java.awt.Color buttonColor
      • buttonHoverColor

        protected static final java.awt.Color buttonHoverColor
      • buttonSize

        protected static final int buttonSize
      • buttonSizeSquared

        protected static final int buttonSizeSquared
      • buttonRoundness

        protected static final float buttonRoundness
      • noButtonRoundness

        protected static final float noButtonRoundness
      • background

        protected static final java.awt.Color background
      • selectedBackground

        protected static final java.awt.Color selectedBackground
      • outlineRoundness

        protected final float outlineRoundness
    • Constructor Detail

      • MultiSelectItem

        public MultiSelectItem​(java.lang.String text,
                               boolean showDelete,
                               boolean fill)
        Create a MultiSelectItem.
        Parameters:
        text - the text to display
        showDelete - true to show the delete button
        fill - true to fill the outline with the background color
    • Method Detail

      • isSelected

        public boolean isSelected()
      • setSelected

        public void setSelected​(boolean selected)
      • setBackgroundSupplier

        public void setBackgroundSupplier​(java.util.function.Function<MultiSelectItem,​java.awt.Color> backgroundSupplier)
      • onMouseMove

        protected void onMouseMove​(java.awt.Point point)
        Updates the hover state of the delete button.
        Parameters:
        point - the mouse cursor position
      • fireDelete

        protected void fireDelete()
        Notifies listeners that the delete button has been clicked.
      • addDeleteListener

        public void addDeleteListener​(java.util.function.Consumer<MultiSelectItem> listener)
        Add a listener to be notified when the delete button is clicked.
      • removeDeleteListener

        public void removeDeleteListener​(java.util.function.Consumer<MultiSelectItem> listener)
        Remove a delete button listener.
      • isShowDelete

        public boolean isShowDelete()
        Get the flag that indicates whether to draw the delete button.
      • isFill

        public boolean isFill()
      • setFill

        public void setFill​(boolean fill)
      • getInsets

        public java.awt.Insets getInsets()
        Overridden to add space for the outline and delete button.
        Overrides:
        getInsets in class javax.swing.JComponent
      • getInsets

        public java.awt.Insets getInsets​(java.awt.Insets insets)
        Overridden to add space for the outline and delete button.
        Overrides:
        getInsets in class javax.swing.JComponent
      • getMinHeight

        public int getMinHeight()
      • paintComponent

        protected void paintComponent​(java.awt.Graphics g)
        Overridden to draw the outline and delete button.
        Overrides:
        paintComponent in class javax.swing.JComponent
      • getOutline

        protected java.awt.Shape getOutline​(int x,
                                            int y,
                                            int width,
                                            int height)
        Get the outline to draw around the text.
        Parameters:
        x - the x offset
        y - the y offset
        width - the width of the outline
        height - the height of the outline
      • drawDeleteButton

        protected void drawDeleteButton​(java.awt.Graphics2D g2d,
                                        boolean hoverEffect)
        Draw the delete button.
        Parameters:
        g2d - the drawing context
        hoverEffect - true if the mouse is over the button