title: JAVA cardLayout. how to call a card from different classes
body: <p>i have a program with a frame that contains a main panel with the cardlayout layout, and i want it to display different cards/panel.</p>
  
  <p>In my case i'm really struggling to call a new card from a button action listener.</p>
  
  <p>I want a new card to appear after i click on a button but none of the codes i put in my action listener displayed the card i wanted.</p>
  
  <p>I know my actionListener work because i did a println inside.</p>
  
  <p>here's my code. i got rid of anything that was unnecessary so it's easier to read. Thanks for the help!</p>
  
  <p>i'll take all advices about code structuration</p>
  
  <p>the mainFrame :</p>
  
  <pre><code>public class MainFrame extends JFrame{
  
  final static String CONNEXION_VIEW = "connexionView"; 
  final static String CONNEXION_FAILED_VIEW = "connexionRefusee"; 
  
  public MainFrame()
  {
      super();
      initialize();
  }
  
  
  private void initialize()
  {
      getMainPanel();
  
      add(getMainPanel());
  
  }
  
  CardLayout cardLayout;
  public CardLayout getCardLayout()
  {
      if (cardLayout == null)
      {
          cardLayout = new CardLayout();
      }
      return cardLayout;
  }
  
  
  JPanel mainPanel;
  public JPanel getMainPanel()
  {
      if (mainPanel == null)
      {
          mainPanel = new JPanel();
  
          mainPanel.setLayout(getCardLayout());
  
          mainPanel.add(CONNEXION_VIEW, getConnexionView());
          mainPanel.add(CONNEXION_FAILED_VIEW, getConnexionFailedView());
  
      }
      return mainPanel;
  }
  
  ConnexionView connexionView;
  protected ConnexionView getConnexionView()
  {
      if (connexionView == null)
      {
          connexionView = new ConnexionView();
      }
      return connexionView;
  }
  
  ConnexionFailedView connexionFailedView;
  protected ConnexionFailedView getConnexionFailedView()
  {
      if (connexionFailedView == null)
      {
          connexionFailedView = new ConnexionFailedView();
      }
      return connexionFailedView;
  }
  </code></pre>
  
  <p>the connexion view, the one with the button to click with the action listener where i want to put my code</p>
  
  <pre><code>public class ConnexionView extends JPanel{
  
  GridBagLayout gbl = new GridBagLayout();
  
  private JButton btnConnexion;
  
  Dimension dimensionBouton = new Dimension(170, 30);
  
  public ConnexionView()
  {
      super();
      initialise();
  }
  
  private void initialise()
  {
      setLayout(gbl);
  
      GridBagConstraints gbcbtnConnexion = new GridBagConstraints();
      gbcbtnConnexion.gridwidth = GridBagConstraints.REMAINDER;
      gbcbtnConnexion.gridheight = GridBagConstraints.REMAINDER;
      gbcbtnConnexion.gridx = 1;
      gbcbtnConnexion.gridy = 2;
      add(getBtnConnexion(), gbcbtnConnexion);
  }
  
  private JButton getBtnConnexion()
  {
      if (btnConnexion == null)
      {
          btnConnexion = new JButton("Connexion");
          btnConnexion.setPreferredSize(dimensionBouton);
          btnConnexion.setMinimumSize(dimensionBouton);
  
          btnConnexion.addMouseListener(new MouseAdapter()
          {
              @Override
              public void mouseClicked(MouseEvent e)
              {
                  /////code to display the connexion_Failed_View
  
                  System.out.println("test");
              }
          });
      }
      return btnConnexion;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and the connexion failed view, the one i want to display after the button is clicked</p>
  
  <pre><code>public class ConnexionFailedView extends JPanel{
  
  public ConnexionFailedView()
  {
      super();
      initialise();
  }
  
  private void initialise()
  {
      setBackground(Color.YELLOW);
  }
  </code></pre>
  
  <p>thanks in advance</p>