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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:35:27+00:00 2026-06-07T17:35:27+00:00

I’m using a cardlayout and I want to make it so that the first

  • 0

I’m using a cardlayout and I want to make it so that the first card has a button and when clicked it will take it to card 2 which has a button that will take it back to card 1. Here is my current code, and I’ve tried putting a few things in the actionPerformed method but I haven’t had any success in getting it to work. Also, I receive a syntax error on “this” on the lines with button1.addActionListener(this); and button2.addActionListener(this); which I assume is because my actionPerformed method isn’t setup correctly. Any help on getting the buttons setup would be greatly appreciated.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Main implements ItemListener {

JPanel cards;

public void addComponentToPane(Container pane) {        
    //create cards
    JPanel card1 = new JPanel();
    JPanel card2 = new JPanel();
    JButton button1 = new JButton("Button 1");
    JButton button2 = new JButton("Button 2");
    button1.addActionListener(this);
    button2.addActionListener(this);
    card1.add(button1);
    card2.add(button2);

    //create panel that contains cards
    cards = new JPanel(new CardLayout());
    cards.add(card1);
    cards.add(card2);        
    pane.add(cards, BorderLayout.CENTER);
}

public void itemStateChanged(ItemEvent evt) {
    CardLayout cl = (CardLayout)(cards.getLayout());
    cl.show(cards, (String)evt.getItem());
}

public static void createAndShowGUI() {
    //create and setup window
    JFrame frame = new JFrame("Frame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //create and setup content pane
    Main main = new Main();
    main.addComponentToPane(frame.getContentPane());

    //display window
    frame.pack();
    frame.setVisible(true);
}

public void actionPerformed(ActionEvent ae) {

}            

public static void main(String[] args) {
    //set look and feel
    try {
        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    } catch (UnsupportedLookAndFeelException ex) {
        ex.printStackTrace();
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
    } catch (InstantiationException ex) {
        ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    }
    //turn off metal's bold fonts
    UIManager.put("swing.boldMetal", Boolean.FALSE);        

    //schedule job for the event dispatch thread creating and showing GUI        
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });     
}   

}

  • 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-07T17:35:28+00:00Added an answer on June 7, 2026 at 5:35 pm
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class CardTest implements ActionListener {
    
        private JPanel cards;
        private JButton button1;
        private JButton button2;
    
        public void addComponentToPane(Container pane) {
            // create cards
            JPanel card1 = new JPanel();
            JPanel card2 = new JPanel();
            button1 = new JButton("Button 1");
            button2 = new JButton("Button 2");
            button1.addActionListener(this);
            button2.addActionListener(this);
            card1.add(button1);
            card2.add(button2);
    
            // create panel that contains cards
            cards = new JPanel(new CardLayout());
            cards.add(card1, "Card 1");
            cards.add(card2, "Card 2");
            pane.add(cards, BorderLayout.CENTER);
        }
    
        public void itemStateChanged(ItemEvent evt) {
            CardLayout cl = (CardLayout) (cards.getLayout());
            cl.show(cards, (String) evt.getItem());
        }
    
        public static void createAndShowGUI() {
            // create and setup window
            JFrame frame = new JFrame("Frame");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // create and setup content pane
            CardTest main = new CardTest();
            main.addComponentToPane(frame.getContentPane());
    
            // display window
            frame.pack();
            frame.setVisible(true);
        }
    
        public void actionPerformed(ActionEvent ae) {
            if (ae.getSource() == button1) {
    
                CardLayout cl = (CardLayout) (cards.getLayout());
                cl.show(cards, "Card 2");
    
            } else if (ae.getSource() == button2) {
    
                CardLayout cl = (CardLayout) (cards.getLayout());
                cl.show(cards, "Card 1");
            }
        }
    
        public static void main(String[] args) {
            // set look and feel
            try {
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InstantiationException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            // turn off metal's bold fonts
            UIManager.put("swing.boldMetal", Boolean.FALSE);
    
            // schedule job for the event dispatch thread creating and showing GUI
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I need a function that will clean a strings' special characters. I do NOT
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.