Multipanes

(c) 2004 JAPISoft
http://www.swingall.com


Multipanes is a container for collapsing/expanding components letting one component visible each time.

Each component is embedded inside a TitlePane. This titled pane has various UI properties like a title, an icon and
a UI content which is the main component. The user can control the space between each titled pane using the VerticalInset
property.

The user can add/remove titled pane at any time using the TitledPaneModel. This is a collection of TitledPane. This
collection can be updated dynamically and the user interface will be updated. A default one is available
calling getModel from the Multipanes. It needn't to implement the TitledPane interface using the BasicTitledPane
class.

Receiving events when a titledPane is collapsed or expanded is available with the TitledPaneListener.

The multipanes class delegates its titled pane look-and-feel to a view implementing the TitledPaneView. By default
this is the DefaultTitledPaneView but the user can also use this one : ArrowTitledPaneView calling setView on the
multipanes instance.

Changing the default look needs to update some properties inside the UIManager with the following keys :
Various usages sample :

public class Demo {
   
    MultiPanes mp = new MultiPanes();
   
    public Demo() {
        JFrame f = new JFrame();

        // Here an optionnal inset for separating each titled pane

        // mp.setVerticalInset( 10 );
   
        // Here a listener for testing when a titled pane is opened or closed
       
        // We set a background for the selected background
        mp.setDefaultSelectedTitledPaneBackground( Color.GRAY );
        // We set a foreground for the selected foreground
        mp.setDefaultSelectedTitledPaneForeground( Color.WHITE );
       
        mp.addTitledPaneListener( new TitledPaneListener() {
           
            /** Called each time the titledpane is closed */
            public void closed( TitledPaneEvent event ) {
                System.out.println( "CLOSED PANE EVENT = " + event.getSelectedTitledPane().getName() );
            }
           
            /** Called each time the titledpane is opened */   
            public void opened( TitledPaneEvent event ) {
                System.out.println( "OPENED PANE EVENT = " + event.getSelectedTitledPane().getName() );               
            }
        } );
       
        // We add 4 titled panes
       
        mp.getModel().addTitledPane(
            new BasicTitledPane( "test1", "Sample 1", new ImageIcon( Demo.class.getResource( "element_selection.png" ) ), "OK", new JTree() ) );

        mp.getModel().addTitledPane(
            new BasicTitledPane("test2", "Sample 2", "ToolTip2", new JTable() ) );

        mp.getModel().addTitledPane(
            new BasicTitledPane( "test3", "Sample 3", new JScrollPane( new JTree() ) ) );

        BasicTitledPane opened = null;
       
        mp.getModel().addTitledPane(
            opened = new BasicTitledPane( "test4", "Sample 4", new Section1() ) );
       
        f.getContentPane().add( mp );

        /**
         * Remove the comment for resetting the default background / foreground
         * UIManager.put(
                "multipanes.bgColor", Color.white );
        UIManager.put(
                "multipanes.bgColor", Color.white );*/
       
       
        // Remove the comment for showing the arrow mode
        // mp.setArrowMode( true );
       
        mp.open( opened );

        f.setSize( 400, 400 );
        f.setVisible( true );

    }
   
    // Here a panel for adding / removing new buttons   
   
    class Section1 extends JPanel implements ActionListener {
        private JButton btn1;
        private JButton btn2;
       
        public Section1() {
            btn1 = new JButton( " Add a new titled pane" );
            btn2 = new JButton( " Remove the last titled pane" );
           
            setBackground( Color.white );
           
            btn1.addActionListener( this );
            btn2.addActionListener( this );
           
            add( btn1 );
            add( btn2 );
        }
       
        public void actionPerformed( ActionEvent e ) {
            if ( e.getSource() == btn1 ) {
                mp.getModel().addTitledPane( new BasicTitledPane( "test", "New Title", "ToolTip", new JLabel( "Ok" ) ) );
            } else {
                if ( mp.getModel().getTitledPaneCount() > 4 ) {
                    mp.getModel().removeTitledPane(
                        mp.getModel().getTitledPaneAt(
                                    mp.getModel().getTitledPaneCount() - 1 ) );
                }
            }
        }
    }



Using a custom look :


public class Demo2 {

    public static void main(String[] args) {

        JFrame f = new JFrame();

        MultiPanes mp = new MultiPanes();

        // We set a background for the selected background
        mp.setDefaultSelectedTitledPaneBackground( new Color( 50, 50, 150 ) );
        // We set a foreground for the selected foreground
        mp.setDefaultSelectedTitledPaneForeground( Color.WHITE );
       
        // Uncomment it for coloring the default titled pane
        // mp.setDefaultTitledPaneBackground( Color.BLACK );
        // mp.setDefaultTitledPaneForeground( Color.WHITE );
        // mp.setDefaultTitledPaneFont( new Font( "Arial", Font.BOLD, 14 ) );
       
        mp.getModel().addTitledPane(
            new BasicTitledPane(
                "test1",
                "Title 1",
                new ImageIcon(Demo.class.getResource("element_selection.png")),
                "OK",
                new JTree()));

        TitledPane test;
       
        /** Remove this comment for coloring the arrow
        UIManager.put(
        "multipanes.arrowTitledPaneView.rightArrowColor", Color.blue );
        UIManager.put(
        "multipanes.arrowTitledPaneView.downArrowColor", Color.white );
        */
       
        mp.getModel().addTitledPane(
            new BasicTitledPane("test2", "Title 2", new JTable()));

        mp.getModel().addTitledPane(
            test = new BasicTitledPane("test3", "Title 3", new JTree()));

        mp.getModel().addTitledPane(
            new BasicTitledPane("test4", "Title 4", new JTable()));
       
        mp.setView( new ArrowTitledPaneView( mp ) );
       
        f.getContentPane().add(mp);

        mp.open( test );   
       
        f.setSize(400, 400);
        f.setVisible(true);
       
        // We open it by default
       
    }
   
}


(c) 2004 JAPISoft