Package swingtree.components
Class JSplitButton
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.AbstractButton
javax.swing.JButton
swingtree.components.JSplitButton
- All Implemented Interfaces:
ImageObserver,ItemSelectable,MenuContainer,Serializable,Accessible,SwingConstants,StylableComponent
- Direct Known Subclasses:
UI.SplitButton
An implementation of a "split button" where the left (larger) side acts like a normal
button and the right side down arrow based button opens an attached
JPopupMenu.
See UIFactoryMethods.splitButton(String), UIFactoryMethods.splitButton(Var) and UIFactoryMethods.splitButton(Var, Event)
for usage in you UIs as well as the UIForSplitButton for more in-depth
configuration (like adding options to the split button for example).
This class raises two events:
SplitButtonActionListener.buttonClicked(java.awt.event.ActionEvent)when the button is clickedSplitButtonActionListener.splitButtonClicked(java.awt.event.ActionEvent)when the split part of the button is clicked
SplitButtonActionListener to
handle these events, however, it is advised to
register events as part of the UIForSplitButton API!- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class javax.swing.JButton
JButton.AccessibleJButtonNested classes/interfaces inherited from class javax.swing.AbstractButton
AbstractButton.AccessibleAbstractButton, AbstractButton.ButtonChangeListenerNested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponentNested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainerNested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy -
Field Summary
Fields inherited from class javax.swing.AbstractButton
actionListener, BORDER_PAINTED_CHANGED_PROPERTY, changeEvent, changeListener, CONTENT_AREA_FILLED_CHANGED_PROPERTY, DISABLED_ICON_CHANGED_PROPERTY, DISABLED_SELECTED_ICON_CHANGED_PROPERTY, FOCUS_PAINTED_CHANGED_PROPERTY, HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY, HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY, ICON_CHANGED_PROPERTY, itemListener, MARGIN_CHANGED_PROPERTY, MNEMONIC_CHANGED_PROPERTY, model, MODEL_CHANGED_PROPERTY, PRESSED_ICON_CHANGED_PROPERTY, ROLLOVER_ENABLED_CHANGED_PROPERTY, ROLLOVER_ICON_CHANGED_PROPERTY, ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY, SELECTED_ICON_CHANGED_PROPERTY, TEXT_CHANGED_PROPERTY, VERTICAL_ALIGNMENT_CHANGED_PROPERTY, VERTICAL_TEXT_POSITION_CHANGED_PROPERTYFields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWFields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTFields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTHFields 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
ConstructorsConstructorDescriptionCreates a button with no set text or icon.JSplitButton(String text) Creates a button with text.JSplitButton(String text, @Nullable Icon icon) Creates a button with initial text and an icon.JSplitButton(Icon icon) Creates a button with an icon. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd aButtonClickedActionListenerto the button.voidDeprecated.voidAdd aSplitButtonClickedActionListenerto the button.Gets the color of the arrow.intGets the size of the arrow.Gets the disabled arrow color.Gets the disabled image to be drawn in the split part.getImage()Gets the image to be drawn in the split part.@Nullable JPopupMenuReturns the JPopupMenu if set, null otherwise.intReturns the separatorSpacing.intSplitwidth is the width of the split part of the button.booleanDeprecated.useisAlwaysPopup()instead.booleanShow the popup menu, if attached, even if the button part is clicked.voidThis method is expected to be implemented as follows:protected voidvoidRemove aButtonClickedActionListenerfrom the button.voidvoidRemove aSplitButtonClickedActionListenerfrom the button.voidsetAlwaysDropDown(boolean alwaysDropDown) Deprecated.usesetAlwaysPopup(boolean)instead.voidsetAlwaysPopup(boolean alwaysPopup) Show the popup menu, if attached, even if the button part is clicked.voidsetArrowColor(Color arrowColor) Set the arrow color.voidsetArrowSize(int arrowSize) Sets the size of the arrow.voidsetDisabledArrowColor(Color disabledArrowColor) Sets the disabled arrow color.voidsetDisabledImage(Image image) Sets the disabled image to draw instead of the triangle.voidSets the image to draw instead of the triangle.voidsetPopupMenu(JPopupMenu popupMenu) Sets the JPopupMenu to be displayed, when the split part of the button is clicked.voidsetSeparatorSpacing(int separatorSpacing) Sets the separatorSpacing.voidsetSplitWidth(int splitWidth) Splitwidth is the width of the split part of the button.voidCertain style configurations require SwingTree to install a custom UI delegate.Methods inherited from class javax.swing.JButton
getAccessibleContext, getUIClassID, isDefaultButton, isDefaultCapable, paramString, removeNotify, setDefaultCapable, updateUIMethods inherited from class javax.swing.AbstractButton
actionPropertyChanged, addActionListener, addChangeListener, addImpl, addItemListener, checkHorizontalKey, checkVerticalKey, configurePropertiesFromAction, createActionListener, createActionPropertyChangeListener, createChangeListener, createItemListener, doClick, doClick, fireActionPerformed, fireItemStateChanged, fireStateChanged, getAction, getActionCommand, getActionListeners, getChangeListeners, getDisabledIcon, getDisabledSelectedIcon, getDisplayedMnemonicIndex, getHideActionText, getHorizontalAlignment, getHorizontalTextPosition, getIcon, getIconTextGap, getItemListeners, getLabel, getMargin, getMnemonic, getModel, getMultiClickThreshhold, getPressedIcon, getRolloverIcon, getRolloverSelectedIcon, getSelectedIcon, getSelectedObjects, getText, getUI, getVerticalAlignment, getVerticalTextPosition, imageUpdate, init, isBorderPainted, isContentAreaFilled, isFocusPainted, isRolloverEnabled, isSelected, paintBorder, removeActionListener, removeChangeListener, removeItemListener, setAction, setActionCommand, setBorderPainted, setContentAreaFilled, setDisabledIcon, setDisabledSelectedIcon, setDisplayedMnemonicIndex, setEnabled, setFocusPainted, setHideActionText, setHorizontalAlignment, setHorizontalTextPosition, setIcon, setIconTextGap, setLabel, setLayout, setMargin, setMnemonic, setMnemonic, setModel, setMultiClickThreshhold, setPressedIcon, setRolloverEnabled, setRolloverIcon, setRolloverSelectedIcon, setSelected, setSelectedIcon, setText, setUI, setVerticalAlignment, setVerticalTextPositionMethods 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, getInsets, getInsets, 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, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, updateMethods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, 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, transferFocusDownCycle, validate, validateTreeMethods 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, transferFocusUpCycleMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface swingtree.style.StylableComponent
paint, paintBackground, paintForeground
-
Constructor Details
-
JSplitButton
Creates a button with initial text and an icon.- Parameters:
text- the text of the buttonicon- the Icon image to display on the button
-
JSplitButton
Creates a button with text.- Parameters:
text- the text of the button
-
JSplitButton
Creates a button with an icon.- Parameters:
icon- the Icon image to display on the button
-
JSplitButton
public JSplitButton()Creates a button with no set text or icon.
-
-
Method Details
-
paintChildren
This method is expected to be implemented as follows:@Override public void paintChildren(Graphics g){ paintForeground(g, ()->super.paintChildren(g)); }- Specified by:
paintChildrenin interfaceStylableComponent- Overrides:
paintChildrenin classJComponent- Parameters:
g- the graphics context to paint on, obtained from the component'sJComponent::paintChildren(Graphics)method.
-
setUISilently
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:
setUISilentlyin interfaceStylableComponent- Parameters:
ui- the UI delegate to set for the component without triggering side effects.
-
getPopupMenu
Returns the JPopupMenu if set, null otherwise.- Returns:
- JPopupMenu
-
setPopupMenu
Sets the JPopupMenu to be displayed, when the split part of the button is clicked.- Parameters:
popupMenu- the menu to display
-
getSeparatorSpacing
public int getSeparatorSpacing()Returns the separatorSpacing. Separator spacing is the space above and below the separator (the line drawn when you hover your mouse over the split part of the button).- Returns:
- the spacing
-
setSeparatorSpacing
public void setSeparatorSpacing(int separatorSpacing) Sets the separatorSpacing. Separator spacing is the space above and below the separator (the line drawn when you hover your mouse over the split part of the button).- Parameters:
separatorSpacing- the spacing
-
isAlwaysPopup
public boolean isAlwaysPopup()Show the popup menu, if attached, even if the button part is clicked.- Returns:
- true if alwaysPopup, false otherwise.
-
setAlwaysPopup
public void setAlwaysPopup(boolean alwaysPopup) Show the popup menu, if attached, even if the button part is clicked.- Parameters:
alwaysPopup- true to show the attached JPopupMenu even if the button part is clicked, false otherwise
-
isAlwaysDropDown
Deprecated.useisAlwaysPopup()instead.Show the dropdown menu, if attached, even if the button part is clicked.- Returns:
- true if alwaysDropdown, false otherwise.
-
setAlwaysDropDown
Deprecated.usesetAlwaysPopup(boolean)instead.Show the dropdown menu, if attached, even if the button part is clicked.- Parameters:
alwaysDropDown- true to show the attached dropdown even if the button part is clicked, false otherwise
-
getArrowColor
Gets the color of the arrow.- Returns:
- the color of the arrow
-
setArrowColor
Set the arrow color.- Parameters:
arrowColor- the color of the arrow
-
getDisabledArrowColor
Gets the disabled arrow color.- Returns:
- color of the arrow if no popup menu is attached.
-
setDisabledArrowColor
Sets the disabled arrow color.- Parameters:
disabledArrowColor- color of the arrow if no popup menu is attached.
-
getSplitWidth
public int getSplitWidth()Splitwidth is the width of the split part of the button.- Returns:
- the width of the split
-
setSplitWidth
public void setSplitWidth(int splitWidth) Splitwidth is the width of the split part of the button.- Parameters:
splitWidth- the width of the split
-
getArrowSize
public int getArrowSize()Gets the size of the arrow.- Returns:
- size of the arrow
-
setArrowSize
public void setArrowSize(int arrowSize) Sets the size of the arrow.- Parameters:
arrowSize- the size of the arrow
-
getImage
Gets the image to be drawn in the split part. If no is set, a new image is created with the triangle.- Returns:
- image
-
setImage
Sets the image to draw instead of the triangle.- Parameters:
image- the image
-
getDisabledImage
Gets the disabled image to be drawn in the split part. If no is set, a new image is created with the triangle.- Returns:
- image
-
setDisabledImage
Sets the disabled image to draw instead of the triangle.- Parameters:
image- the new image to use
-
paintComponent
- Overrides:
paintComponentin classJComponent
-
addSplitButtonActionListener
Deprecated.Adds anSplitButtonActionListenerto the button.- Parameters:
l- theActionListenerto be added
-
removeSplitButtonActionListener
Deprecated.Removes anSplitButtonActionListenerfrom the button. If the listener is the currently setActionfor the button, then theActionis set tonull.- Parameters:
l- the listener to be removed
-
addButtonClickedActionListener
Add aButtonClickedActionListenerto the button. This listener will be notified whenever the button part is clicked.- Parameters:
l- the listener to add.
-
removeButtonClickedActionListener
Remove aButtonClickedActionListenerfrom the button.- Parameters:
l- the listener to remove.
-
addSplitButtonClickedActionListener
Add aSplitButtonClickedActionListenerto the button. This listener will be notified whenever the split part is clicked.- Parameters:
l- the listener to add.
-
removeSplitButtonClickedActionListener
Remove aSplitButtonClickedActionListenerfrom the button.- Parameters:
l- the listener to remove.
-
addButtonClickedActionListener(swingtree.components.action.ButtonClickedActionListener)oraddSplitButtonClickedActionListener(swingtree.components.action.SplitButtonClickedActionListener)instead.