Class JXCollapsiblePane

  • All Implemented Interfaces:
    ImageObserver, MenuContainer, Serializable, Accessible, Scrollable

    public class JXCollapsiblePane
    extends JXPanel
    JXCollapsiblePane provides a component which can collapse or expand its content area with animation and fade in/fade out effects. It also acts as a standard container for other Swing components.

    The JXCollapsiblePane has a "content pane" that actually holds the displayed contents. This means that colors, fonts, and other display configuration items must be set on the content pane.

    
     // to set the font
     collapsiblePane.getContentPane().setFont(font);
     // to set the background color
     collapsiblePane.getContentPane().setBackground(Color.RED);
     
     
    For convenience, the add and remove methods forward to the content pane. The following code shows to ways to add a child to the content pane.
    
     // to add a child
     collapsiblePane.getContentPane().add(component);
     // to add a child
     collapsiblePane.add(component);
     
     
    To set the content pane, do not use add, use setContentPane(Container).

    In this example, the JXCollapsiblePane is used to build a Search pane which can be shown and hidden on demand.

     
     JXCollapsiblePane cp = new JXCollapsiblePane();
    
     // JXCollapsiblePane can be used like any other container
     cp.setLayout(new BorderLayout());
    
     // the Controls panel with a textfield to filter the tree
     JPanel controls = new JPanel(new FlowLayout(FlowLayout.LEFT, 4, 0));
     controls.add(new JLabel("Search:"));
     controls.add(new JTextField(10));
     controls.add(new JButton("Refresh"));
     controls.setBorder(new TitledBorder("Filters"));
     cp.add("Center", controls);
    
     JXFrame frame = new JXFrame();
     frame.setLayout(new BorderLayout());
    
     // Put the "Controls" first
     frame.add("North", cp);
    
     // Then the tree - we assume the Controls would somehow filter the tree
     JScrollPane scroll = new JScrollPane(new JTree());
     frame.add("Center", scroll);
    
     // Show/hide the "Controls"
     JButton toggle = new JButton(cp.getActionMap().get(JXCollapsiblePane.TOGGLE_ACTION));
     toggle.setText("Show/Hide Search Panel");
     frame.add("South", toggle);
    
     frame.pack();
     frame.setVisible(true);
     
     

    The JXCollapsiblePane has a default toggle action registered under the name TOGGLE_ACTION. Bind this action to a button and pressing the button will automatically toggle the pane between expanded and collapsed states. Additionally, you can define the icons to use through the EXPAND_ICON and COLLAPSE_ICON properties on the action. Example

     
     // get the built-in toggle action
     Action toggleAction = collapsible.getActionMap().
       get(JXCollapsiblePane.TOGGLE_ACTION);
    
     // use the collapse/expand icons from the JTree UI
     toggleAction.putValue(JXCollapsiblePane.COLLAPSE_ICON,
                           UIManager.getIcon("Tree.expandedIcon"));
     toggleAction.putValue(JXCollapsiblePane.EXPAND_ICON,
                           UIManager.getIcon("Tree.collapsedIcon"));
     
     

    Note: JXCollapsiblePane requires its parent container to have a LayoutManager using getPreferredSize() when calculating its layout (example VerticalLayout, BorderLayout).

    Author:
    rbair (from the JDNC project), Frederic Lavigne, Karl George Schaefer
    See Also:
    Serialized Form
    • Field Detail

      • ANIMATION_STATE_KEY

        public static final String ANIMATION_STATE_KEY
        Used when generating PropertyChangeEvents for the "animationState" property. The PropertyChangeEvent will takes the following different values for PropertyChangeEvent.getNewValue():
        • reinit every time the animation starts
        • expanded when the animation ends and the pane is expanded
        • collapsed when the animation ends and the pane is collapsed
        See Also:
        Constant Field Values
      • TOGGLE_ACTION

        public static final String TOGGLE_ACTION
        JXCollapsible has a built-in toggle action which can be bound to buttons. Accesses the action through collapsiblePane.getActionMap().get(JXCollapsiblePane.TOGGLE_ACTION).
        See Also:
        Constant Field Values
      • COLLAPSE_ICON

        public static final String COLLAPSE_ICON
        The icon used by the "toggle" action when the JXCollapsiblePane is expanded, i.e the icon which indicates the pane can be collapsed.
        See Also:
        Constant Field Values
      • EXPAND_ICON

        public static final String EXPAND_ICON
        The icon used by the "toggle" action when the JXCollapsiblePane is collapsed, i.e the icon which indicates the pane can be expanded.
        See Also:
        Constant Field Values
    • Constructor Detail

      • JXCollapsiblePane

        public JXCollapsiblePane()
        Constructs a new JXCollapsiblePane with a JXPanel as content pane and a vertical VerticalLayout with a gap of 2 pixels as layout manager and a vertical orientation.
      • JXCollapsiblePane

        public JXCollapsiblePane​(JXCollapsiblePane.Direction direction)
        Constructs a new JXCollapsiblePane with a JXPanel as content pane and the specified direction.
        Parameters:
        direction - the direction to collapse the container
      • JXCollapsiblePane

        public JXCollapsiblePane​(LayoutManager layout)
        Constructs a new JXCollapsiblePane with a JPanel as content pane and the given LayoutManager and a vertical orientation
      • JXCollapsiblePane

        public JXCollapsiblePane​(JXCollapsiblePane.Direction direction,
                                 LayoutManager layout)
        Constructs a new JXCollapsiblePane with a JPanel as content pane and the given LayoutManager and orientation. A vertical orientation enables a vertical VerticalLayout with a gap of 2 pixels as layout manager. A horizontal orientation enables a horizontal HorizontalLayout with a gap of 2 pixels as layout manager
        Parameters:
        direction - the direction this pane collapses
        layout - of this collapsible pane
    • Method Detail

      • setContentPane

        public void setContentPane​(Container contentPanel)
        Sets the content pane of this JXCollapsiblePane. The contentPanel should implement Scrollable and return true from Scrollable.getScrollableTracksViewportHeight() and Scrollable.getScrollableTracksViewportWidth(). If the content pane fails to do so and a JScrollPane is added as a child, it is likely that the scroll pane will never correctly size. While it is not strictly necessary to implement Scrollable in this way, the default content pane does so.
        Parameters:
        contentPanel - the container delegate used to hold all of the contents for this collapsible pane
        Throws:
        IllegalArgumentException - if contentPanel is null
      • getContentPane

        public Container getContentPane()
        Returns:
        the content pane
      • addImpl

        protected void addImpl​(Component comp,
                               Object constraints,
                               int index)
        Overriden to redirect call to the content pane.
        Overrides:
        addImpl in class Container
      • remove

        public void remove​(Component comp)
        Overriden to redirect call to the content pane
        Overrides:
        remove in class Container
      • remove

        public void remove​(int index)
        Overriden to redirect call to the content pane.
        Overrides:
        remove in class Container
      • removeAll

        public void removeAll()
        Overriden to redirect call to the content pane.
        Overrides:
        removeAll in class Container
      • setAnimated

        public void setAnimated​(boolean animated)
        If true, enables the animation when pane is collapsed/expanded. If false, animation is turned off.

        When animated, the JXCollapsiblePane will progressively reduce (when collapsing) or enlarge (when expanding) the height of its content area until it becomes 0 or until it reaches the preferred height of the components it contains. The transparency of the content area will also change during the animation.

        If not animated, the JXCollapsiblePane will simply hide (collapsing) or show (expanding) its content area.

        Parameters:
        animated -
      • isAnimated

        public boolean isAnimated()
        Returns:
        true if the pane is animated, false otherwise
        See Also:
        setAnimated(boolean)
      • setDirection

        public void setDirection​(JXCollapsiblePane.Direction direction)
        Changes the direction of this collapsible pane. Doing so changes the layout of the underlying content pane. If the chosen direction is vertical, a vertical layout with a gap of 2 pixels is chosen. Otherwise, a horizontal layout with a gap of 2 pixels is chosen.
        Parameters:
        direction - the new JXCollapsiblePane.Direction for this collapsible pane
        Throws:
        IllegalStateException - when this method is called while a collapsing/restore operation is running
        See Also:
        getDirection()
      • isCollapsed

        public boolean isCollapsed()
        Returns:
        true if the pane is collapsed, false if expanded
      • setCollapsed

        public void setCollapsed​(boolean val)
        Expands or collapses this JXCollapsiblePane.

        If the component is collapsed and val is false, then this call expands the JXCollapsiblePane, such that the entire JXCollapsiblePane will be visible. If isAnimated() returns true, the expansion will be accompanied by an animation.

        However, if the component is expanded and val is true, then this call collapses the JXCollapsiblePane, such that the entire JXCollapsiblePane will be invisible. If isAnimated() returns true, the collapse will be accompanied by an animation.

        See Also:
        isAnimated(), setAnimated(boolean)
      • getMinimumSize

        public Dimension getMinimumSize()
        A collapsible pane always returns its preferred size for the minimum size to ensure that the collapsing happens correctly.

        To query the minimum size of the contents user getContentPane().getMinimumSize().

        Overrides:
        getMinimumSize in class JComponent
        Returns:
        the preferred size of the component
      • setMinimumSize

        public void setMinimumSize​(Dimension minimumSize)
        Forwards to the content pane.
        Overrides:
        setMinimumSize in class JComponent
        Parameters:
        minimumSize - the size to set on the content pane
      • getPreferredSize

        public Dimension getPreferredSize()
        The critical part of the animation of this JXCollapsiblePane relies on the calculation of its preferred size. During the animation, its preferred size (specially its height) will change, when expanding, from 0 to the preferred size of the content pane, and the reverse when collapsing.
        Overrides:
        getPreferredSize in class JComponent
        Returns:
        this component preferred size