Class MultiSelectItem
- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- javax.swing.JComponent
-
- javax.swing.JLabel
-
- io.github.jonestimd.swing.component.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. TheaddDeleteListener(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 theMultiSelectField
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
-
-
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.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
-
-
Constructor Summary
Constructors Constructor Description MultiSelectItem(java.lang.String text, boolean showDelete, boolean fill)
Create aMultiSelectItem
.
-
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
-
-
-
-
Field Detail
-
GAP
public static final int GAP
- See Also:
- Constant Field Values
-
CROSS_SIZE
protected static final double CROSS_SIZE
- See Also:
- Constant Field Values
-
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
-
buttonGeometry
protected final MultiSelectItem.ButtonGeometry buttonGeometry
-
-
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 classjavax.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 classjavax.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 classjavax.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 offsety
- the y offsetwidth
- the width of the outlineheight
- the height of the outline
-
drawDeleteButton
protected void drawDeleteButton(java.awt.Graphics2D g2d, boolean hoverEffect)
Draw the delete button.- Parameters:
g2d
- the drawing contexthoverEffect
- true if the mouse is over the button
-
-