Class JSplitButton

All Implemented Interfaces:
ImageObserver, ItemSelectable, MenuContainer, Serializable, Accessible, SwingConstants, StylableComponent
Direct Known Subclasses:
UI.SplitButton

public class JSplitButton extends JButton implements Serializable, StylableComponent
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:

  1. SplitButtonActionListener.buttonClicked(java.awt.event.ActionEvent) when the button is clicked
  2. SplitButtonActionListener.splitButtonClicked(java.awt.event.ActionEvent) when the split part of the button is clicked
You can implement SplitButtonActionListener to handle these events, however, it is advised to register events as part of the UIForSplitButton API!
See Also:
  • Constructor Details

    • JSplitButton

      public JSplitButton(String text, @Nullable Icon icon)
      Creates a button with initial text and an icon.
      Parameters:
      text - the text of the button
      icon - the Icon image to display on the button
    • JSplitButton

      public JSplitButton(String text)
      Creates a button with text.
      Parameters:
      text - the text of the button
    • JSplitButton

      public JSplitButton(Icon icon)
      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

    • paint

      public void paint(Graphics g)
      This method is expected to be implemented as follows within a component extension which ought to be made compatible with SwingTree.
            @Override
            public void paint(Graphics g){
                paintBackground(g, ()->super.paint(g));
            }
        
      Specified by:
      paint in interface StylableComponent
      Overrides:
      paint in class JComponent
      Parameters:
      g - the graphics context to paint on, obtained from the component's JComponent.paint(Graphics) method.
    • 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)
      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.
    • getPopupMenu

      public @Nullable JPopupMenu getPopupMenu()
      Returns the JPopupMenu if set, null otherwise.
      Returns:
      JPopupMenu
    • setPopupMenu

      public void setPopupMenu(JPopupMenu popupMenu)
      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 public boolean isAlwaysDropDown()
      Deprecated.
      use isAlwaysPopup() instead.
      Show the dropdown menu, if attached, even if the button part is clicked.
      Returns:
      true if alwaysDropdown, false otherwise.
    • setAlwaysDropDown

      @Deprecated public void setAlwaysDropDown(boolean alwaysDropDown)
      Deprecated.
      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

      public Color getArrowColor()
      Gets the color of the arrow.
      Returns:
      the color of the arrow
    • setArrowColor

      public void setArrowColor(Color arrowColor)
      Set the arrow color.
      Parameters:
      arrowColor - the color of the arrow
    • getDisabledArrowColor

      public Color getDisabledArrowColor()
      Gets the disabled arrow color.
      Returns:
      color of the arrow if no popup menu is attached.
    • setDisabledArrowColor

      public void setDisabledArrowColor(Color disabledArrowColor)
      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

      public Image 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

      public void setImage(Image image)
      Sets the image to draw instead of the triangle.
      Parameters:
      image - the image
    • getDisabledImage

      public 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

      public void setDisabledImage(Image image)
      Sets the disabled image to draw instead of the triangle.
      Parameters:
      image - the new image to use
    • paintComponent

      protected void paintComponent(Graphics g)
      Overrides:
      paintComponent in class JComponent
    • addSplitButtonActionListener

      @Deprecated public void addSplitButtonActionListener(SplitButtonActionListener l)
      Adds an SplitButtonActionListener to the button.
      Parameters:
      l - the ActionListener to be added
    • removeSplitButtonActionListener

      @Deprecated public void removeSplitButtonActionListener(SplitButtonActionListener l)
      Removes an SplitButtonActionListener from the button. If the listener is the currently set Action for the button, then the Action is set to null.
      Parameters:
      l - the listener to be removed
    • addButtonClickedActionListener

      public void addButtonClickedActionListener(ButtonClickedActionListener l)
      Add a ButtonClickedActionListener to the button. This listener will be notified whenever the button part is clicked.
      Parameters:
      l - the listener to add.
    • removeButtonClickedActionListener

      public void removeButtonClickedActionListener(ButtonClickedActionListener l)
      Remove a ButtonClickedActionListener from the button.
      Parameters:
      l - the listener to remove.
    • addSplitButtonClickedActionListener

      public void addSplitButtonClickedActionListener(SplitButtonClickedActionListener l)
      Add a SplitButtonClickedActionListener to the button. This listener will be notified whenever the split part is clicked.
      Parameters:
      l - the listener to add.
    • removeSplitButtonClickedActionListener

      public void removeSplitButtonClickedActionListener(SplitButtonClickedActionListener l)
      Remove a SplitButtonClickedActionListener from the button.
      Parameters:
      l - the listener to remove.