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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:22:27+00:00 2026-05-23T11:22:27+00:00

I have written this simple Cardlayout example with Splitpane, Combobox and few other panels

  • 0

I have written this simple Cardlayout example with Splitpane, Combobox and few other panels containing buttons and label.

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class splitpane_test extends JFrame implements ItemListener {

    private JPanel contentPane;
    final static String BUTTONPANEL = "Card with JButtons";
    final static String TEXTPANEL = "Card with JTextField";
    JPanel cards;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    splitpane_test frame = new splitpane_test();
                    //frame.addComponentToPane(frame.getContentPane());
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public splitpane_test() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JSplitPane splitPane = new JSplitPane();
        contentPane.add(splitPane, BorderLayout.CENTER);


        JPanel comboBoxPane = new JPanel();
        String comboBoxItems[] = {BUTTONPANEL, TEXTPANEL};
        JComboBox cb = new JComboBox(comboBoxItems);
        cb.setEditable(false);
        cb.addItemListener(this);
        comboBoxPane.add(cb);
        splitPane.setLeftComponent(comboBoxPane);

        //Create the "cards".
        JPanel card1 = new JPanel();
        card1.add(new JButton("Button 1"));
        card1.add(new JButton("Button 2"));
        card1.add(new JButton("Button 3"));

        JPanel card2 = new JPanel();
        card2.add(new JTextField("TextField", 20));

        //Create the panel that contains the "cards".
        cards = new JPanel();
        cards.add(card1, BUTTONPANEL);
        cards.add(card2, TEXTPANEL);
        splitPane.setRightComponent(cards);
        cards.setLayout(new CardLayout(0, 0));
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        // TODO Auto-generated method stub
        System.out.print("Event Triggered \n");
        CardLayout cl = (CardLayout) (cards.getLayout());
        cl.show(cards, TEXTPANEL);
    }
}

I can see the splitpane with combobox on left and other cardlayout panels on right.
when i change the combobox items nothing is happening on right size.
In order to verify if iam hitting the cardout i used the
System.out.print(“Event Triggered \n”);
but the surprising thing i have seen is that its displaying twice for each combobox item change as if its calling twice

Event Triggered
Event Triggered

Can you please suggest me what iam doing wrong here and why event triggered is getting hit twice.
Thanks for all your time and help.

  • 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-23T11:22:28+00:00Added an answer on May 23, 2026 at 11:22 am
    1. Set the layout manager before adding components.

    2. Two items are changing state; one is being deselected, the other selected. You would improve your debugging output with the following:

      System.out.println("Event Triggered: " + e);

    You’ll also need to account for the event switching the selection back–not all changes in the combo box should select TEXTPANEL.

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

Sidebar

Related Questions

I have written this simple bit of jQuery that swaps out the containing ul's
I am new to Python and I have written this simple script: #!/usr/bin/python3 import
I have written this simple script in python: import gtk window = gtk.Window() window.set_size_request(800,
This is a simple script I have written to test command line argument handling:
I have written a secure TCP server in .NET. This was basically as simple
I am learning Python for the past few days and I have written this
I have written a simple mysql search form that searches between two dates. This
I've written this fiddle to demo the problem. I have a simple form. When
I want to automate a fairly simple task. For this I have written a
I have written this simple C program that changes the rotation file name. Eg:

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.