Results 1 to 7 of 7
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default How can I switch JPanels using CardLayout please? What am I doing wrong ?

    I have a MainPanel that is held by my frame:

    Java Code:
    public class MainPanel extends JPanel {
    	
    	public LanguagePanel langPanel = new LanguagePanel(this);
    	public SecondPanel secPanel = new SecondPanel();
    	
    	JFrame frame;
    	
    	public MainPanel() {
    		langPanel = new LanguagePanel(this);
    		this.setLayout(new CardLayout());
    		this.add(langPanel, "LanguagePanel");
    		this.add(secPanel, "SecondPanel");
    	}
    }
    Here is my LanguagePanel code:

    Java Code:
    public class LanguagePanel extends JPanel
    {
    	private JButton turkishButton = new JButton("Turkish");
    	private JButton englishButton = new JButton("English");
    	private JPanel mainPanel;
    	private MainFrame mainFrame;
    	
    	public LanguagePanel(JPanel panel)
    	{
    		mainPanel = panel;
    		englishButton.addActionListener(new EnglishButtonListener());
    		this.add(englishButton);
    		this.add(turkishButton);
    	}
    	
    	class EnglishButtonListener implements ActionListener
    	{
    
    		@Override
    		public void actionPerformed(ActionEvent e) 
    		{
    			mainPanel.setVisible(mainPanel.setVisible(mainPanel.secPanel));
    		}
    	}
    }
    I am expecting to get the secondPanel when I click on English button on my LanguagePanel.
    But it doesn't work.

    Please help ?

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How can I switch JPanels using CardLayout please? What am I doing wrong ?

    You can switch cards like this:

    Java Code:
    CardLayout cl = (CardLayout) mainPanel.getLayout();
    cl.show(mainPanel, "SecondPanel");
    Refer to the guide:

    How to Use CardLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: How can I switch JPanels using CardLayout please? What am I doing wrong ?

    Thank you for your reply, I was looking at that page for the last 30 minutes, but no help. But this just worked fine, thanks.

    There is no way to do this without

    Java Code:
     public LanguagePanel(JPanel panel)
        {
            mainPanel = panel;
            // more code here...

  4. #4
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: How can I switch JPanels using CardLayout please? What am I doing wrong ?

    I have, but my situation is a bit different, isn't it ?

    If everything is in just one file, I can see how it works.

    When 2 classes are involved, I get confused.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How can I switch JPanels using CardLayout please? What am I doing wrong ?

    You can always get a component's parent via getParent(), get its layout via getLayout(), etc...

  6. #6
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: How can I switch JPanels using CardLayout please? What am I doing wrong ?

    So I need to get the outerobject of the EnglishButtonListener, which is a JPanel, and get its Parent Component right ?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How can I switch JPanels using CardLayout please? What am I doing wrong ?

    Though not even needed here:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class MainPanel extends JPanel {
       
       public LanguagePanel langPanel = new LanguagePanel(this);
       public SecondPanel secPanel = new SecondPanel();
        
       JFrame frame;
        
       public MainPanel() {
           langPanel = new LanguagePanel(this);
           this.setLayout(new CardLayout());
           this.add(langPanel, LanguagePanel.NAME);
           this.add(secPanel, SecondPanel.NAME);
       }
       
       private static void createAndShowGui() {
          MainPanel mainPanel = new MainPanel();
    
          JFrame frame = new JFrame("MainPanel");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    class LanguagePanel extends JPanel
    {
       public static final String NAME = "Language Panel";
        private JButton turkishButton = new JButton("Turkish");
        private JButton englishButton = new JButton("English");
        private JPanel mainPanel;
        // private MainFrame mainFrame;
         
        public LanguagePanel(JPanel panel)
        {
            mainPanel = panel;
            englishButton.addActionListener(new EnglishButtonListener());
            this.add(englishButton);
            this.add(turkishButton);
        }
         
        class EnglishButtonListener implements ActionListener
        {
     
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                CardLayout cLayout = (CardLayout) mainPanel.getLayout();
                cLayout.show(mainPanel, SecondPanel.NAME);
            }
        }
    }
    
    class SecondPanel extends JPanel {
       public static final String NAME = "Second Panel";
       
       public SecondPanel() {
          add(new JLabel("Second Panel"));
       }
    }

Similar Threads

  1. CardLayout with JPanels
    By Angelo in forum AWT / Swing
    Replies: 17
    Last Post: 12-15-2011, 06:50 PM
  2. JFrame and CardLayout -- Wrong Parent
    By AutoRobin in forum New To Java
    Replies: 11
    Last Post: 04-21-2011, 12:39 PM
  3. Populating CardLayout JPanels with External classes
    By fred2482 in forum New To Java
    Replies: 5
    Last Post: 12-21-2009, 06:08 AM
  4. Switch JPanels in a single JFrame
    By atom86 in forum AWT / Swing
    Replies: 8
    Last Post: 09-23-2009, 09:30 AM
  5. What is wrong with my switch??
    By soc86 in forum New To Java
    Replies: 3
    Last Post: 11-20-2008, 12:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •