Class TLcyAlwaysFitJToolBar

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, SwingConstants

public class TLcyAlwaysFitJToolBar extends JToolBar
Extension of JToolBar that nicely handles the case when not enough space is available to display all tool bar items. If not all tool bar items fit, a button is added that shows a popup with the remaining items.

Screenshot

Limitations

  • Layout constraints of added components are ignored. This is usually no problem because tool bar buttons don't have any.
  • Components which show a popup are not supported in the remaining items popup window. They will appear in the remaining items popup, but their popup menu cannot be shown. This is a limitation of the JDK, which does not support to have popups inside popups.
  • When manipulating the tool bar items when the tool bar is already visible, the methods suffixed with 'RRI' (Respect Remaining Items) need to be used instead of their java.awt.Container counterparts. This is required because this java.awt.Container automatically removes components from itself if they don't fit. Also, if needed, a button to show the remaining items popup is automatically added. As a result, manipulating the children might behave different from what you expect.

    For example, if 5 buttons are added, it might happen that only one button is actually part of the tool bar, followed by a button to show the remaining items. The 4 other buttons reside in the remaining items popup. If a 6th button is now added using the regular add method, it appears after the remaining items button. If the addRRI method is used, it will appear, as expected, as the last button in the remaining items popup.

See Also:
  • Constructor Details

    • TLcyAlwaysFitJToolBar

      public TLcyAlwaysFitJToolBar()
      Creates a new TLcyAlwaysFitJToolBar, ready to be used.
      See Also:
    • TLcyAlwaysFitJToolBar

      public TLcyAlwaysFitJToolBar(int aOrientation)
      Creates a new TLcyAlwaysFitJToolBar, ready to be used.
      Parameters:
      aOrientation - The orientation of the tool bar. Either HORIZONTAL or VERTICAL.
      See Also:
    • TLcyAlwaysFitJToolBar

      public TLcyAlwaysFitJToolBar(String aName)
      Creates a new TLcyAlwaysFitJToolBar, ready to be used.
      Parameters:
      aName - The name for the title of the undocked tool bar.
      See Also:
    • TLcyAlwaysFitJToolBar

      public TLcyAlwaysFitJToolBar(String aName, int aOrientation)
      Creates a new TLcyAlwaysFitJToolBar, ready to be used.
      Parameters:
      aName - The name for the title of the undocked tool bar.
      aOrientation - The orientation of the tool bar. Either HORIZONTAL or VERTICAL.
      See Also:
  • Method Details

    • updateUI

      public void updateUI()
      Overrides:
      updateUI in class JToolBar
    • getRemainingItemsIcon

      public ILcdIcon getRemainingItemsIcon()
      Returns the icon that is used to show the remaining items.
      Returns:
      the icon that is used to show the remaining items.
      See Also:
    • setRemainingItemsIcon

      public void setRemainingItemsIcon(ILcdIcon aIcon)
      Sets the icon that is used to show the remaining items.
      Parameters:
      aIcon - The icon.
    • getMinimumSize

      public Dimension getMinimumSize()
      Overrides:
      getMinimumSize in class JComponent
    • getPreferredSize

      public Dimension getPreferredSize()
      Overrides:
      getPreferredSize in class JComponent
    • getMaximumSize

      public Dimension getMaximumSize()
      Overrides:
      getMaximumSize in class JComponent
    • addRRI

      public void addRRI(Component comp)
      Adds the given component to this tool bar, respecting the remaining items pop up. See class comment for more info.
      Parameters:
      comp - The component to add.
      See Also:
    • addRRI

      public void addRRI(Component comp, int index)
      Adds the given component to this tool bar, respecting the remaining items pop up. See class comment for more info.
      Parameters:
      comp - The component to add.
      index - The index for inserting the component.
      See Also:
    • removeRRI

      public void removeRRI(int index)
      Removes the given component from this tool bar, respecting the remaining items pop up. See class comment for more info.
      Parameters:
      index - The index of the component to remove.
      See Also:
    • removeRRI

      public void removeRRI(Component comp)
      Removes the given component from this tool bar, respecting the remaining items pop up. See class comment for more info.
      Parameters:
      comp - The component to remove.
      See Also:
    • removeAllRRI

      public void removeAllRRI()
      Removes all components from this tool bar, respecting the remaining items pop up. See class comment for more info.
      See Also:
    • getComponentsRRI

      public Component[] getComponentsRRI()
      Returns an array with all tool bar components. This includes the components currently part of this JToolBar, but also those that are (hidden in) the remaining items popup. It does not include the button to shows the remaining items.
      Returns:
      An array with all visible and hidden components.
      See Also:
    • getComponentRRI

      public Component getComponentRRI(int aIndex)
      Functionally equivalent to getComponentsRRI()[aIndex]. See class comment for more info.
      Parameters:
      aIndex - The index of the component to retrieve. 0 <= aIndex < getComponentCountRRI()
      Returns:
      The component at the given index.
      See Also:
    • getComponentCountRRI

      public int getComponentCountRRI()
      Functionally equivalent to getComponentsRRI().length. See class comment for more info.
      Returns:
      The amount of child components, including those in the remaining items popup.
      See Also:
    • invalidate

      public void invalidate()
      Overrides:
      invalidate in class Container
    • setBounds

      public void setBounds(int x, int y, int width, int height)
      Overrides:
      setBounds in class Component
    • doLayout

      public void doLayout()
      Overrides:
      doLayout in class Container
    • createRemainingItemsPopupContent

      protected JComponent createRemainingItemsPopupContent(Component[] aRemainingItems)
      Builds the content for the remaining items popup. An implementation could for example be to create a new JToolBar, and add the given components to it.
      Parameters:
      aRemainingItems - The components that should be displayed in the remaining items popup.
      Returns:
      The content for the remaining items popup.
    • getPreferredRemainingItemsPopupLocation

      protected Point getPreferredRemainingItemsPopupLocation(Component aInvoker, Component aContent)
      Fetches the desired location for the top left of the popup containing the remaining items. This preferred location might not be respected to avoid that the popup partially falls off screen (see clampRemainingItemsPopupOnScreen.
      Parameters:
      aInvoker - The component that triggered displaying the popup, typically the 'arrow' button.
      aContent - The content that needs to be displayed in the popup. Can for example be used to retrieve the preferred size.
      Returns:
      The desired location for the popup, in screen coordinate space. For example aInvoker.getLocationOnScreen() plus aInvoker.getHeight().
    • clampRemainingItemsPopupOnScreen

      protected Point clampRemainingItemsPopupOnScreen(Component aInvoker, Component aContent, Point aPreferredLocation)
      Clamps the popup on screen. This is useful to avoid that it is only partially visible, or to prevent that it is spread over multiple screens.
      Parameters:
      aInvoker - The component that triggered displaying the popup, typically the 'arrow' button.
      aContent - The content that needs to be displayed in the popup. Can for example be used to retrieve the preferred size.
      aPreferredLocation - The preferred location in screen coordinate space, as provided by getPreferredRemainingItemsPopupLocation.
      Returns:
      The actual location in screen coordinate space, that respects screen boundaries. If clamping the popup on screen is not desired, aPreferredLocation can simply be returned.
    • isRemainingItemsPopupVisible

      protected boolean isRemainingItemsPopupVisible()
      Returns true if the popup is currently on screen. This method is protected as there is normally no need to call it from outside this class as the popup is automatically shown and hidden.
      Returns:
      true if the popup is currently on screen, false otherwise.
      See Also:
    • setRemainingItemsPopupVisible

      protected void setRemainingItemsPopupVisible(boolean aVisible)
      Sets the visibility of the remaining items popup. This method is protected as there is normally no need to call it from outside this class as the popup is automatically shown and hidden.
      Parameters:
      aVisible - true to make the popup visible, false to hide it.
    • createToolBarPanel

      public static Component createToolBarPanel(Component aComponent)
      Utility method to wrap a given component so that it plays nice on a tool bar.

      For example, if you add a JComboBox on a tool bar, you'll notice that it receives all extra available space, making the combo much larger as desired. Using this method to add a wrapper panel around the JComboBox resolves this issue.

      Parameters:
      aComponent - The component to wrap.
      Returns:
      The component, wrapped in a panel, to improve tool bar layout.