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

  • Home
  • SEARCH
  • 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 8005055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:06:37+00:00 2026-06-04T17:06:37+00:00

I looked at a code example that used this code: cl.show(cardPanel, + (currentCard)); But

  • 0

I looked at a code example that used this code:

cl.show(cardPanel, "" + (currentCard));

But when I use show I get a message in Eclipse that it’s deprecated and I wonder if there is another way to show the different cards in the CardLayout when I click on the buttons? Below is the code for my CardLayout class. Suggestions are also welcome if some part of the code are bad practise. Thanks!

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

public class CardLayoutTest extends JFrame implements ActionListener {

// Ref
 private JPanel cardPanel, jp1, jp2, buttonPanel;
 private JLabel jl1, jl2;
 private JButton btn1, btn2;
 private CardLayout cardLayout;

// Konstruktor
 public CardLayoutTest()
 {
     setTitle("Test med CardLayout");
     setSize(600,400);

     cardPanel = new JPanel();
     buttonPanel = new JPanel();

     cardPanel.setLayout(cardLayout);

     jp1 = new JPanel();
     jp2 = new JPanel();

     jl1 = new JLabel("Card 1");
     jl2 = new JLabel("Card 2");

     jp1.add(jl1);
     jp2.add(jl2);

     cardPanel.add(jp1, "1");
     cardPanel.add(jp2, "2");

     btn1 = new JButton("Show Card 1");
     btn2 = new JButton("Show Card 2");

     buttonPanel.add(btn1);
     buttonPanel.add(btn2);

     getContentPane().add(cardPanel, BorderLayout.NORTH);
     getContentPane().add(buttonPanel, BorderLayout.SOUTH);

     btn1.addActionListener(this);
 }

     public void actionPerformed(ActionEvent event)
     {
        // ??? Show card 1 ???

        // ??? Show card 2 ???
     }

 public static void main(String[] args) {
     CardLayoutTest frame = new CardLayoutTest();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    } 
}
  • 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-04T17:06:39+00:00Added an answer on June 4, 2026 at 5:06 pm
    • I can’t see that Java7 show(Container parent, String name) or Java6 show(Container parent, String name) is depreciated

    • depends if currentCard returns String from cl.show(cardPanel, "" + (currentCard));

    EDIT (I tried your code example)

    1.you forgot to initialize most important variable

    private CardLayout cardLayout = new CardLayout();
    

    2.then SSCCE could be

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class CardLayoutTest extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private JPanel cardPanel, jp1, jp2, buttonPanel;
        private JLabel jl1, jl2;
        private JButton btn1, btn2;
        private CardLayout cardLayout = new CardLayout();
    
        public CardLayoutTest() {
            setTitle("Test med CardLayout");
            setSize(400, 300);
            cardPanel = new JPanel();
            buttonPanel = new JPanel();
            cardPanel.setLayout(cardLayout);
            jp1 = new JPanel();
            jp2 = new JPanel();
            jl1 = new JLabel("Card 1");
            jl2 = new JLabel("Card 2");
            jp1.add(jl1);
            jp2.add(jl2);
            cardPanel.add(jp1, "1");
            cardPanel.add(jp2, "2");
            btn1 = new JButton("Show Card 1");
            btn1.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    cardLayout.show(cardPanel, "1");
                }
            });
            btn2 = new JButton("Show Card 2");
            btn2.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    cardLayout.show(cardPanel, "2");
                }
            });
            buttonPanel.add(btn1);
            buttonPanel.add(btn2);
            add(cardPanel, BorderLayout.NORTH);
            add(buttonPanel, BorderLayout.SOUTH);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    CardLayoutTest frame = new CardLayoutTest();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've looked at http://code.google.com/appengine/docs/java/urlfetch/overview.html but the code does not show a pooling example, i
My colleague's code looked like this: void copy(std::string const& s, char *d) { for(int
I just discovered a bug where the code looked something like this: char *foo
I looked through some code and noticed that the convention was to turn pointer
I had some fortran code that looked something like: subroutine foo(mx,my,mz) real pts(3,mx,my,mz) end
I looked through previous questions to this effect, but my situation is slightly different...
Summary I have looked over the code the SpiderMonkey 'shell' application uses to create
I'm trying to load a text file from res/raw. I've looked at several code
I've looked in the gtk source code and the header height is private. I've
I have looked at the generated designer code of Form s and UserControl s,

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.