title: how to replace jpanel with anotherjpanel on jframe when button is clicked
body: <p>I want the JPanel at the center of the frame to be replaced when the corresponding buttons present in the JPanel to the left of the frame are pressed. 
  I am using cardlayout for replacing the panels in the center.
  here is the sample code that i used for changing panels on button click ,but it doesn't work. can anyone let me know whats wrong in the code.</p>
  
  <p>Library extends JFrame</p>
  
  <pre><code> Public Library(){
            Container container = getContentPane();
        container.setLayout( new BorderLayout() );
        setExtendedState(JFrame.MAXIMIZED_BOTH);
  
        banner = new BannerPanel();
        container.add(banner,BorderLayout.NORTH);
  
        MenuButtons = new MenuButtonPanel();
        container.add(MenuButtons,BorderLayout.WEST);
  
        SelectedButtonPanel = new SelectedPanel(container);
  
        setLocationRelativeTo(null);
        setVisible( true );
        init();
  
        setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
  </code></pre>
  
  <p>MenuButtonPanel extends JPanel and has multiple buttons in it. The inner class of it ButtonHandler implements hte actionListner for the buttons</p>
  
  <pre><code> //inner class
     class ButtonEventHandler implements ActionListener {     
          public void actionPerformed( ActionEvent event ){
  
              SelectedPanel selectObj = new SelectedPanel();
  
              if("about".equals(event.getActionCommand()))
                  {
                      selectObj.showReplacePanel("about");
  
                  }
                  else if("checkout".equals(event.getActionCommand()))
                  {
                      selectObj.showReplacePanel("checkout");
                  }
                  else if("search".equals(event.getActionCommand()))
                  {
                      selectObj.showReplacePanel("search");
                  }
  </code></pre>
  
  <p>The SelectedPanel should display the replaced Jpanel in the center of the frame</p>
  
  <pre><code>    public SelectedPanel() {}
  
  public SelectedPanel(Container framePane)
  {
      addSelectedPanel(framePane);
  }
  public void addSelectedPanel(Container framePane)
  {
      cardPanel = new JPanel();
      //set layout of panel
      cardPanel.setLayout(new CardLayout());
      cardPanel.setBorder(null);
      cardPanel.setForeground(new Color(0, 0, 0));
      //this.selObj = selObj;
              aboutPanel = new About();
              checkoutPanel = new Checkout();
              searchPanel = new Search();
                             cardPanel.add(aboutPanel,ABOUTBUTTON);
              cardPanel.add(checkoutPanel,CHECKOUTBUTTON);
              cardPanel.add(searchPanel, SEARCHBUTTON);
                               framePane.add(cardPanel, BorderLayout.CENTER);  }
  </code></pre>
  
  <p>The selectedPanel class also has method showReplacePanel() which is called in the actionPerformed of the buttonClick.</p>
  
  <pre><code>public void showReplacePanel(String selObj)
  {
      System.out.println("cardlayout entered");
      CardLayout cl = (CardLayout)(cardPanel.getLayout());
      System.out.println("cardlayout entered");
      switch(selObj){        
      case "about":
  
          cl.show(cardPanel,ABOUTBUTTON);
                  break;          
      case "search":
          //this.repPanel = searchPanel;
          cl.show(cardPanel,SEARCHBUTTON);
          break;
      case "checkout":
          cl.show(cardPanel,CHECKOUTBUTTON);
          //this.repPanel = checkoutPanel;
          break;
  </code></pre>