Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7551711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:33:59+00:00 2026-05-30T10:33:59+00:00

so I have this jFrame with a Panel. Inside that panel there are two

  • 0

so I have this jFrame with a Panel.
Inside that panel there are two more panels and the layout is set to cards.
Inside one of those two panels there is a button.
How can I change the panel thats being displayed when that button is pressed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:34:00+00:00Added an answer on May 30, 2026 at 10:34 am

    Try this code snippet, hope the comments might help you understand the sequence.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /* Here we are first declaring our class that will act as the
     * base for other panels or in other terms the base for CardLayout.
     */
    
    public class CardLayoutTest
    {
        private static final String CARD_JBUTTON =  "Card JButton";
        private static final String CARD_JTEXTFIELD = "Card JTextField";    
        private static final String CARD_JRADIOBUTTON = "Card JRadioButton";
    
        private static void createAndShowGUI()
        {
            JFrame frame = new JFrame("Card Layout Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
    
            // This JPanel is the base for CardLayout for other JPanels.
            final JPanel contentPane = new JPanel();
            contentPane.setLayout(new CardLayout(20, 20));
    
            /* Here we be making objects of the Window Series classes
             * so that, each one of them can be added to the JPanel 
             * having CardLayout. 
             */
            Window1 win1 = new Window1();
            contentPane.add(win1, CARD_JBUTTON);
            Window2 win2 = new Window2();
            contentPane.add(win2, CARD_JTEXTFIELD);
            Window3 win3 = new Window3();
            contentPane.add(win3, CARD_JRADIOBUTTON);
    
            /* We need two JButtons to go to the next Card
             * or come back to the previous Card, as and when
             * desired by the User.
             */
            JPanel buttonPanel = new JPanel(); 
            final JButton previousButton = new JButton("PREVIOUS");
            previousButton.setBackground(Color.BLACK);
            previousButton.setForeground(Color.WHITE);
            final JButton nextButton = new JButton("NEXT");
            nextButton.setBackground(Color.RED);
            nextButton.setForeground(Color.WHITE);
            buttonPanel.add(previousButton);
            buttonPanel.add(nextButton);
    
            /* Adding the ActionListeners to the JButton,
             * so that the user can see the next Card or
             * come back to the previous Card, as desired.
             */
            previousButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                    cardLayout.previous(contentPane);
                }
            });
            nextButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                    cardLayout.next(contentPane);   
                }
            });
    
            // Adding the contentPane (JPanel) and buttonPanel to JFrame.
            frame.add(contentPane, BorderLayout.CENTER);
            frame.add(buttonPanel, BorderLayout.PAGE_END);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowGUI();
                }
            });
        }
    } 
    
    class Window1 extends JPanel
    {
        /*
         * Here this is our first Card of CardLayout, which will
         * be added to the contentPane object of JPanel, which
         * has the LayoutManager set to CardLayout.
         * This card consists of Two JButtons.
         */  
        private ActionListener action;
    
        public Window1() 
        {
            init();
        }
    
        private void init() 
        {
            final JButton clickButton = new JButton("CLICK ME");
            final JButton dontClickButton = new JButton("DON\'T CLICK ME");     
    
            action = new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    if (ae.getSource() == clickButton)
                    {
                        JOptionPane.showMessageDialog(null, "Hello there dude!"
                                                    , "Right Button", JOptionPane.INFORMATION_MESSAGE);
                    }
                    else if (ae.getSource() == dontClickButton)
                    {
                        JOptionPane.showMessageDialog(null, "I told you not to click me!"
                                                            , "Wrong Button", JOptionPane.PLAIN_MESSAGE);
                    }
                }
            };
    
            clickButton.addActionListener(action);
            dontClickButton.addActionListener(action);
    
            add(clickButton);
            add(dontClickButton);
        }
    }
    
    class Window2 extends JPanel implements ActionListener 
    {
        /*
         * Here this is our second Card of CardLayout, which will
         * be added to the contentPane object of JPanel, which
         * has the LayoutManager set to CardLayout.
         * This card consists of a JLabel and a  JTextField
         * with GridLayout.
         */  
    
        private JTextField textField;
    
        public Window2() 
        {
            init();
        }
    
        private void init() 
        {
            setLayout(new GridLayout(1, 2));
            JLabel userLabel = new JLabel("Your Name : ");
            textField = new JTextField();
            textField.addActionListener(this);
    
            add(userLabel);
            add(textField);
        }
    
        public void actionPerformed(ActionEvent e) 
        {            
            if (textField.getDocument().getLength() > 0)
                JOptionPane.showMessageDialog(null, "Your Name is : " + textField.getText()
                                                                                , "User\'s Name : ", JOptionPane.QUESTION_MESSAGE);
        }
    }
    
    class Window3 extends JPanel
    {
        /*
         * Here this is our third Card of CardLayout, which will
         * be added to the contentPane object of JPanel, which
         * has the LayoutManager set to CardLayout.
         * This card consists of Two JLabels and two JCheckBox
         * with GridLayout.
         */  
        private ActionListener state;
    
        public Window3()
        {
            init();
        }
    
        public void init()
        {
            setLayout(new GridLayout(2, 2));
            JLabel maleLabel = new JLabel("MALE", JLabel.CENTER);
            final JCheckBox maleBox = new JCheckBox();
            JLabel femaleLabel = new JLabel("FEMALE", JLabel.CENTER);
            final JCheckBox femaleBox = new JCheckBox();
    
            state = new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    if (maleBox == (JCheckBox) ae.getSource())
                    {
                        femaleBox.setSelected(false);
                        JOptionPane.showMessageDialog(null, "Congrats you are a Male"
                                                    , "Gender : ", JOptionPane.INFORMATION_MESSAGE);                            
                    }
                    else if (femaleBox == (JCheckBox) ae.getSource())
                    {
                        maleBox.setSelected(false);
                        JOptionPane.showMessageDialog(null, "Congrats you are a Female"
                                                , "Gender : ", JOptionPane.INFORMATION_MESSAGE);                        
                    }
                }
            };
    
            maleBox.addActionListener(state);
            femaleBox.addActionListener(state);
            add(maleLabel);
            add(maleBox);
            add(femaleLabel);
            add(femaleBox);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one's a tough one - I have a JFrame that generates JTextFields. When
This is the problem, I have a class MainWindow that extends JFrame in one
I have this class called PollFrame that extends JFrame in a file called PollFrame.java
I have write this code for displaying some set of colors from panel: import
I have a JFrame, in this JFrame I have a JPanel that I draw
I have made two panels and then added in third panel. How can I
I Have two files. One extends JFrame, and another Extends JPanel. Whenever I change
I have a JFrame inside of which is a jpanel that im using as
I have a JFrame with four components: three JPanels and a JTabbedPane. Two panels
I have this code: public class Window extends JFrame { public Window(){ ... JButton

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.