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 9274671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:24:28+00:00 2026-06-18T16:24:28+00:00

I am trying to work out how to navigate across multiple panels in swing.

  • 0

I am trying to work out how to navigate across multiple panels in swing. I would like to do this by using the CardLayout as opposed to using glass panels as from what I have read it seems that this is the correct tool for this job (however, feel free to correct me if you know otherwise). I have written a test case which almost achieves this but falls short on 2 fronts. It uses the depreciated “show()” method and furthermore after it switches to the second card the buttons from card1 begin to mysteriously float through to the surface again!

public class test extends JPanel implements ActionListener{
    final static int extraWindowWidth = 100;
    JButton jbtnOne = new JButton("Button 1");
        JPanel cardPanel = new JPanel(new CardLayout());
        JPanel card1 = new JPanel();
        JPanel card2 = new JPanel();

    public void addComponentToPane(Container pane) {

        //Create the "cards".
        JPanel card1 = new JPanel() {
            public Dimension getPreferredSize() {
                Dimension size = super.getPreferredSize();
                size.width += extraWindowWidth;
                return size;
            }
        };


        card1.add(jbtnOne);
        card1.add(new JButton("Button 2"));
        card1.add(new JButton("Button 3"));     
        card2.add(new JTextField("TextField", 20));

        cardPanel.add(card1, "card1");
        cardPanel.add(card2, "card2");

        pane.add(cardPanel, BorderLayout.CENTER);
        jbtnOne.addActionListener(this);
    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("TabDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        test demo = new test();
        demo.addComponentToPane(frame.getContentPane());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource() ==  jbtnOne){
            System.out.println("HERE");
            card2.show();
        }
    }
}
  • 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-06-18T16:24:29+00:00Added an answer on June 18, 2026 at 4:24 pm

    The CardLayout is managing the components so you will need to call show on the CardLayout instead of JPanel#show:

    CardLayout cardLayout = (CardLayout) cardPanel.getLayout();
    cardLayout.show(cardPanel, "card2");
    

    Alternatively, when switching card components, you could also use

    cardLayout.next(cardPanel);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying work out the boarder of a bufferedimage after using an affine translation
I am trying work out with MERGE statment to Insert / Update Dimension Table
Im trying to work out how to get the value from one setting in
I trying to work out what regex will match filenames in quotes. Eg. blah
i am trying to work out the following problem in Java (although it could
I am trying to work out why I can not access the nested function
I am trying to work out how I can insert the string End into
I am trying to work out the best practice for my problem. I have
I am trying to work out an IF statement and getting lost. I am
I'm trying to work out the correct pattern to use for a Singleton that

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.