Results 1 to 6 of 6
  1. #1
    m3dabbag is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Post Multiple CardLayouts Problem

    Hey, I'm pretty new to Java and I was wondering if anyone can help me out with figuring out a problem with using multiple cardlayouts. What I want to do is to have a combobox that has sections in it. Whenever the user chooses a section from the combobox, i want everything in the mainpanel (which has a cardlayout) to show. In the mainpanel, I have another combobox that contains testcategories. When the user chooses a test category, a bunch of checkboxes should show (which are placed in a jpanel with cardlayout). The problem is that when I choose anything from the testCategory combobox, the cards don't update and I can't see the checkboxes associated with whatever option they chose.

    Here is the code (Please note that this works the way it is but still needs some work). Thanks in advance!

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*; 
    import java.util.Vector;
    
    public class Testing implements ItemListener {
    	
    	private JFrame frame;
    	private JComboBox testCategoryList;
    	JComboBox sectionBox;
    	private JPanel cards; 
    	JPanel test1Panel;
    	JPanel test2Panel;
    	JPanel mainPanel = new JPanel(new CardLayout());
    	private static Vector<JPanel> subPanels = new Vector<JPanel>();
    	
    	
    	String[] testCategories = {"Test 1", "Test 2"};
    	String[] sectionList = {"Section 1", "Section 2"};
    	String selectedTests;
    	
    	public Testing() {
    		//Main Frame
    		frame = new JFrame("Testing");
    		frame.getContentPane().setLayout(new BorderLayout());
    		frame.addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent we) {
    				System.exit(0);
    			}
    		});
    		
    		sectionBox = new JComboBox(sectionList);
    		sectionBox.addItemListener(this);
    		
    		//JPanel comboBoxPanel = new JPanel();
    		testCategoryList = new JComboBox(testCategories);
    		testCategoryList.addItemListener(this);
    		//comboBoxPanel.add(testCategoryList);
    		
    		test1Panel = new JPanel();
    		test1Panel.add(new JCheckBox("test1_1"));
    		test1Panel.add(new JCheckBox("Test1_2"));
    		test1Panel.add(new JCheckBox("Test1_3"));
    		test1Panel.add(new JCheckBox("Test1_4"));
    		
    		test2Panel = new JPanel();
    		test2Panel.add(new JCheckBox("test2_1"));
    		test2Panel.add(new JCheckBox("test2_2"));
    		test2Panel.add(new JCheckBox("test2_3"));
    		test2Panel.add(new JCheckBox("test2_4"));
    		
    		cards = new JPanel(new CardLayout());
    		cards.add(test1Panel, "Test 1");
    		cards.add(test2Panel, "Test 2");
    		
    		for(int i = 0; i < testCategories.length; i++) {
    			subPanels.add((JPanel)cards.getComponent(i));
    		}
    		
    		mainPanel.add(subPanels.get(0), "Section 1");
    		mainPanel.add(subPanels.get(1), "Section 2");
    		mainPanel.add(subPanels.get(0), "Section 1");
    		mainPanel.add(subPanels.get(1), "Section 2");
    		
    		frame.getContentPane().add(sectionBox, BorderLayout.PAGE_START);
    		frame.getContentPane().add(testCategoryList, BorderLayout.BEFORE_LINE_BEGINS);
    		//frame.getContentPane().add(cards, BorderLayout.CENTER);
    		frame.getContentPane().add(mainPanel);
    		frame.pack();
    		frame.setSize(600, 500);
    		frame.setVisible(true);
    	}
    	
    	public void itemStateChanged(ItemEvent event) {
    		if(event.getSource().equals(testCategoryList)) {
    			CardLayout newCard = (CardLayout)(cards.getLayout());
    			newCard.show(cards, (String)event.getItem());
    			frame.repaint();
    		} else {
    			System.out.println("Section Changed");
    		}
    	}
    	
    	public static void main(String args[]) {
    		new Testing();
    	}
    }
    Last edited by m3dabbag; 03-21-2011 at 02:55 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,303
    Rep Power
    20

    Default

    A Component can have only one parent. When you add test1Panel and test2Panel to mainPanel (twice!?!) they are automatically removed from cards.

    Start simple. Create a panel with just two cards, each of which has an identifying label, and a combo box that allows you to change between them. Expand on that without again attempting to add the same component to more than one container.

    db

  3. #3
    m3dabbag is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    sorry, which part are you talking about? Is it this?

    mainPanel.add(subPanels.get(0), "Section 1");
    mainPanel.add(subPanels.get(1), "Section 2");
    mainPanel.add(subPanels.get(0), "Section 1");
    mainPanel.add(subPanels.get(1), "Section 2");

    I know how to take care of a simple cardlayout but my problem is with a cardlayout within a cardlayout. If you could guide me somehow to create a cardlayout within a cardlayout with the proper initialization/components that would be awesome. I've been trying for a couple days now and I haven't had much success. Thanks.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,303
    Rep Power
    20

    Default

    What part of
    A Component can have only one parent.
    didn't you understand?

    Don't try 'reusing' components, it doesn't work that way.

    db
    Last edited by DarrylBurke; 03-22-2011 at 04:37 PM.

  5. #5
    m3dabbag is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    The thing is, i need to show the testCateogry combobox and all the cards associated with it when the user chooses an item from the section combobox. So I need to show everything according to what they choose. If I don't put the cards in a panel, then I can't add them to the mainPanel and I need to show whatever is in the mainPanel according to what section they choose. It's basically a system where certain panels are shown according to what section they choose and certain panels are shown according to what category they choose within the section.

  6. #6
    m3dabbag is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    I managed to solve the problem. All I did was add the cards panel to another jpanel called subpanels and then added the subpanels to the mainPanel and linked it with each section.

Similar Threads

  1. problem with htttpSession with multiple browsers at a time
    By sandeepsai39 in forum New To Java
    Replies: 1
    Last Post: 11-01-2010, 11:48 AM
  2. Problem with multiple string in classes
    By sjaakie in forum New To Java
    Replies: 3
    Last Post: 10-10-2010, 02:48 PM
  3. multiple panes problem
    By grease_monkey1986 in forum AWT / Swing
    Replies: 1
    Last Post: 05-09-2010, 01:06 PM
  4. Problem while using the multiple panels in swings
    By anand206 in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2010, 04:33 PM
  5. Problem calling multiple instances of a class
    By virex in forum New To Java
    Replies: 1
    Last Post: 03-02-2010, 03:03 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •