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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:55:39+00:00 2026-06-12T16:55:39+00:00

> I have a jframe defined in a package ‘abc’. this jframe acts as

  • 0
  • > I have a jframe defined in a package ‘abc’. this jframe acts as a main class too.
  • > the same package ‘abc’ also contain 4 jpanels(panel1,panel2,panel3,panel4) defined in different java classes.

how one should call/show these different jpanels from main jframe class at user clicks on different buttons ?
what if I’ve got 40-50 such jpanels.what would be the most efficient way to toggle these panels from the main class ?

  • 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-12T16:55:40+00:00Added an answer on June 12, 2026 at 4:55 pm

    This is a very simple example that uses next() and one button to change panels.

    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.CardLayout;
    
    import java.awt.Color;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    
    import java.awt.event.ActionListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.border.Border;
    
    public class TestCards extends JFrame {
    
        private final CardLayout cl = new CardLayout();
        private final JPanel cards = new JPanel(cl);
        private final Border border = BorderFactory.createEmptyBorder(60, 60, 60, 60);
    
        public TestCards() {
    
            JPanel contentPane = new JPanel();
            contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
            contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
    
            JPanel panel1 = new JPanel(new GridBagLayout());
            panel1.setBorder(border);
            panel1.setBackground(Color.RED);
            panel1.add(new JLabel("Card 1"));
            cards.add(panel1, "First Panel");
    
            JPanel panel2 = new JPanel(new GridBagLayout());
            panel2.setBorder(border);
            panel2.setBackground(Color.GREEN);
            panel2.add(new JLabel("Card 2"));
            cards.add(panel2, "Second Panel");
    
            JPanel panel3 = new JPanel(new GridBagLayout());
            panel3.setBorder(border);
            panel3.setBackground(Color.BLUE);
            panel3.add(new JLabel("Card 3"));
            cards.add(panel3, "Third Panel");
    
            JButton controlButton = new JButton("Switch");
            controlButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    cl.next(cards);
                }
            });
            JPanel controlPanel = new JPanel();
            controlPanel.setBackground(Color.LIGHT_GRAY);
            controlPanel.add(controlButton);
    
            contentPane.add(cards);
            contentPane.add(controlPanel);
    
            cl.show(cards, "First Panel");
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    TestCards frame = new TestCards();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }
    

    An alternative (among many) would be to use 3 buttons where each one shows a specific panel (by using show instead of next).

    See this for more info.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JFrame that is the main class of the program. I want
I have jFrame class MainForm which contains main() , placePanel(panel) and drop down menu.
I have a JFrame which holds and JScrollPane. The JScrollPane itself holds a class
I have a JFrame, in this JFrame I have a JPanel that I draw
I am using my predefined inherited Focus Traversal Class For My JFrame I have
I have a Jframe class that has a login and password fields. When loggin
I have a JFrame that contains a JTextPane. The purpose of this JTextPane is
I have a JFrame that opens a JFileChooser via an action event. When this
I have a JFrame (well, a class which extends JFrame) and I want to
I'm doing another Java project for my class. In this assignment, we have to

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.