Results 1 to 2 of 2
  1. #1
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default CardLayout Question

    I have a method that generates JPanels for my JFrame with CardLayout. I thought I could get around making 150 cards that all look the same (just different words) by using one empty Jpanel and adding the generated one(s) to it. Problem is it doesn't work as planned. Is there a way to tell the previously empty Jpanel to revert to it's original state? Here's the code that adds the method-generated JPanel to the LessonHolderPanel. (the empty one)
    Java Code:
    		JComboBox cbLesson = new JComboBox(cbLessonList);
    		cbLesson.setBackground(Color.YELLOW);
    		cbLesson.setFont(new Font("Tahoma", Font.BOLD, 15));
    		cbLesson.setPreferredSize(new Dimension(120, 20));
    		ItemListener LessonListener = new ItemListener(){
    			@Override
    			public void itemStateChanged(ItemEvent e) { 
    				int state = e.getStateChange();			
    				System.out.println("State = " + state);
    				if (state == 1){
    					lessonNum = Utility.LSNumber(e.getItem().toString());
    					CardLayout cl = (CardLayout)deck.getLayout();
    					JPanel temp = Utility.FetchLesson(lessonSetNum, lessonNum);
    					LessonHolderPanel.add(temp, BorderLayout.CENTER);
    					cl.show(deck, "LessonHolder");
    					System.out.println(Integer.toString(lessonSetNum) +
    							" " + Integer.toString(lessonNum));
    				}
    			}	
    		};
    		cbLesson.addItemListener(LessonListener);
    Notes: The above code is in the method that puts everything in the JFrame. The variables lessonSetNum and lessonNum are class variables. Utility is a class that houses many of the methods for this class. lessonSetNum and lessonNum are changing when I make selections in the JComboBoxes. The code for the other combobox is here:
    Java Code:
    		JComboBox cbLS = new JComboBox(cbLSList);
    		cbLS.setFont(new Font("Tahoma", Font.BOLD, 15));
    		cbLS.setBackground(Color.YELLOW);		
    		cbLS.setPreferredSize(new Dimension(120, 20));
    		ItemListener LessonSetListener = new ItemListener(){
    			@Override
    			public void itemStateChanged(ItemEvent e) {
    				int state = e.getStateChange();
    				if (state == 1){
    					lessonSetNum = Utility.LSNumber(e.getItem().toString());
    					lessonNum = 0;
    				}
    			}	
    		};
    		cbLS.addItemListener(LessonSetListener);
    How can I get LessonHolderPanel to dump the lesson (temp) it gets in that first bit of code?
    TIA
    Last edited by NotTooOld; 04-28-2013 at 02:05 PM.

  2. #2
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: CardLayout Question

    Nevermind... simple answer is I forgot to revalidate the LessonHolderPanel.
    Java Code:
    		ItemListener LessonListener = new ItemListener(){
    			@Override
    			public void itemStateChanged(ItemEvent e) { 
    				int state = e.getStateChange();			
    				if (state == 1){
    					LessonHolderPanel.removeAll();
    					lessonNum = Utility.LSNumber(e.getItem().toString());
    					CardLayout cl = (CardLayout)deck.getLayout();
    					JPanel temp = Utility.FetchLesson(lessonSetNum, lessonNum);
    					LessonHolderPanel.add(temp, BorderLayout.CENTER);
    					LessonHolderPanel.revalidate();
    					cl.show(deck, "LessonHolder");
    				}
    			}	
    		};
    		cbLesson.addItemListener(LessonListener);
    works like a charm!

Similar Threads

  1. CardLayout Help
    By David M. in forum New To Java
    Replies: 4
    Last Post: 08-06-2011, 03:57 AM
  2. Question about cardlayout
    By scheffetz in forum New To Java
    Replies: 1
    Last Post: 04-02-2011, 09:05 PM
  3. Help with CardLayout
    By Kyle227 in forum New To Java
    Replies: 4
    Last Post: 05-28-2010, 02:03 AM
  4. cardlayout question
    By newbie123 in forum New To Java
    Replies: 1
    Last Post: 04-06-2010, 12:53 AM
  5. Regarding CardLayout
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-07-2008, 08:52 PM

Posting Permissions

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