Results 1 to 6 of 6
Thread: Multiple CardLayouts Problem
- 03-21-2011, 02:52 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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.
- 03-21-2011, 05:04 PM #2
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
- 03-21-2011, 06:47 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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.
- 03-22-2011, 01:09 AM #4
What part of
didn't you understand?A Component can have only one parent.
Don't try 'reusing' components, it doesn't work that way.
dbLast edited by DarrylBurke; 03-22-2011 at 04:37 PM.
- 03-22-2011, 03:13 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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.
- 03-22-2011, 06:26 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
problem with htttpSession with multiple browsers at a time
By sandeepsai39 in forum New To JavaReplies: 1Last Post: 11-01-2010, 11:48 AM -
Problem with multiple string in classes
By sjaakie in forum New To JavaReplies: 3Last Post: 10-10-2010, 02:48 PM -
multiple panes problem
By grease_monkey1986 in forum AWT / SwingReplies: 1Last Post: 05-09-2010, 01:06 PM -
Problem while using the multiple panels in swings
By anand206 in forum AWT / SwingReplies: 3Last Post: 03-23-2010, 04:33 PM -
Problem calling multiple instances of a class
By virex in forum New To JavaReplies: 1Last Post: 03-02-2010, 03:03 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks