Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: CardLayout does not show my content

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default CardLayout does not show my content

    I have a JFrame, a JSplitPane as the container of the frame.
    On the left side of the splitpane, there are two radio buttons (more to come, but I am stuck).
    On the right side, there is a CardLayout. This CardLayout has two 'cards' (JPanels, actually). And to test if it shows the panels when I switch them, I put one checkbox each.

    But it does not show anything. I cannot think anything to do. I tried to put only the checkboxes thinking maybe it only accepts components, but it didn't work.

    This is the GUI class I have. I have commented what I am trying to do so that you don't have to read everything. Also, it does not have the imports and it is started from another class.
    Since I am just testing, I created a project for this, so the main class main method just starts this.
    public class SolveSomehow extends JFrame{
     
    		private JSplitPane splitPane;
    		private JPanel leftPanel,rightPanel,panelOne,panelTwo;
    		private JCheckBox cbTwo,cbOne,cbCardOne,cbCardTwo;
    		private CardLayout cardLay;
     
    		private final String ONE = "PanelOne";
    		private final String TWO = "PanelTwo";
     
    		public SolveSomehow() {
    			setResizable(false);
    			setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    			setBounds(100, 100, 450, 300);
    			// I use WindowBuilder as GUI designer and it had a JPanel as the container.
    			// I deleted and added splitPane.
    			splitPane = new JSplitPane();
    			splitPane.setResizeWeight(0.5);
    			setContentPane(splitPane);
     
     
    			//Next 6 lines I put panels to the left and right side of the splitpane.
    			leftPanel = new JPanel();
    			splitPane.setLeftComponent(leftPanel);
    			leftPanel.setLayout(null); //WindowBuilder made this null but it actually has absoluteLayout
     
    			rightPanel = new JPanel();
    			splitPane.setRightComponent(rightPanel);
     
    			// I have to define the cardLay before the rightPanel, or else it does not recognize it
    			// as the parent. [show(Container parent, String name);]
    			cardLay = new CardLayout(0, 0);
    			rightPanel.setLayout(cardLay);
     
    			//Next 6 lines are about the left side of the splitPane. I add my checkboxes
    			cbTwo = new JCheckBox("Box Two");
    			cbTwo.setBounds(6, 7, 97, 23);
    			leftPanel.add(cbTwo);
     
    			cbOne = new JCheckBox("Box One");
    			cbOne.setBounds(6, 34, 97, 23);
    			leftPanel.add(cbOne);
     
    			// The two panels I try to add to the cardLayout.
    			panelOne = new JPanel(new FlowLayout());
    			panelTwo = new JPanel(new FlowLayout());
     
     
    			// Adding the checkboxes to the panels before adding them into the cardlayout.
    			cbCardOne = new JCheckBox("Panel One");
    			cbCardTwo = new JCheckBox("Panel Two");
     
    			//Finally, boxes are added to the panel
    			panelOne.add(cbCardOne);
    			panelTwo.add(cbCardTwo);
     
    			//Panels are added to the cardLayout
    			cardLay.addLayoutComponent(panelOne, ONE);			
    			cardLay.addLayoutComponent(panelTwo, TWO);
     
    			//I want it to show the relevant panel when the checkbox gets the focus. 
    			//So I wrote this Focus Adapter.
    			cbTwo.addFocusListener(new FocusAdapter(){
    				@Override
    				public void focusGained(FocusEvent arg0) {
    					System.out.println("Did it change?"); //Testing if 'focus' is what I thought,
    					cardLay.show(rightPanel, ONE);		//And it is what I thought.
    				}
    			});
     
    			cbOne.addFocusListener(new FocusAdapter(){
    				@Override
    				public void focusGained(FocusEvent arg0) {
    					System.out.println("Didn't it change?");
    					cardLay.show(rightPanel, TWO);
    				}
    			});
     
    		}
    	}

    What am I doing wrong?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: CardLayout does not show my content

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: CardLayout does not show my content

    Norm, I hope this is enough for you to help me. The attached file includes my source file and a compiled app. The source and app only have the codes about the CardLayout.
    Attached Files Attached Files

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: CardLayout does not show my content

    Please post the code here in the thread. It needs to compile, execute and show the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: CardLayout does not show my content

    Okay. This is my main class, only calls the GUI:
    public class SolveHere {
     
    	public static void main(String[] args) {
    		SolveSomehow m = new SolveSomehow();
    		m.setVisible(true);
     
    	}
    }

    This is the SolveSomehow class, which creates the GUI:
    import java.awt.CardLayout;
    import java.awt.FlowLayout;
    import java.awt.event.FocusAdapter;
    import java.awt.event.FocusEvent;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JSplitPane;
     
    public class SolveSomehow extends JFrame{
     
     
    		private static final long serialVersionUID = 10277105675385182L;
    		private JSplitPane splitPane;
    		private JPanel leftPanel,rightPanel,panelOne,panelTwo;
    		private JCheckBox cbTwo,cbOne,cbCardOne,cbCardTwo;
    		private CardLayout cardLay;
     
    		private final String ONE = "PanelOne";
    		private final String TWO = "PanelTwo";
     
    		public SolveSomehow() {
    			setResizable(false);
    			setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    			setBounds(100, 100, 450, 300);
    			// I use WindowBuilder as GUI designer and it had a JPanel as the container.
    			// I deleted and added splitPane.
    			splitPane = new JSplitPane();
    			splitPane.setResizeWeight(0.5);
    			setContentPane(splitPane);
     
     
    			//Next 6 lines I put panels to the left and right side of the splitpane.
    			leftPanel = new JPanel();
    			splitPane.setLeftComponent(leftPanel);
    			leftPanel.setLayout(null); //WindowBuilder made this null but it actually has absoluteLayout
     
    			rightPanel = new JPanel();
    			splitPane.setRightComponent(rightPanel);
     
    			// I have to define the cardLay before the rightPanel, or else it does not recognize it
    			// as the parent. [show(Container parent, String name);]
    			cardLay = new CardLayout(0, 0);
    			rightPanel.setLayout(cardLay);
     
    			//Next 6 lines are about the left side of the splitPane. I add my checkboxes
    			cbTwo = new JCheckBox("Box Two");
    			cbTwo.setBounds(6, 7, 97, 23);
    			leftPanel.add(cbTwo);
     
    			cbOne = new JCheckBox("Box One");
    			cbOne.setBounds(6, 34, 97, 23);
    			leftPanel.add(cbOne);
     
    			// The two panels I try to add to the cardLayout.
    			panelOne = new JPanel(new FlowLayout());
    			panelTwo = new JPanel(new FlowLayout());
     
     
    			// Adding the checkboxes to the panels before adding them into the cardlayout.
    			cbCardOne = new JCheckBox("Panel One");
    			cbCardTwo = new JCheckBox("Panel Two");
     
    			//Finally, boxes are added to the panel
    			panelOne.add(cbCardOne);
    			panelTwo.add(cbCardTwo);
     
    			//Panels are added to the cardLayout
    			cardLay.addLayoutComponent(panelOne, ONE);			
    			cardLay.addLayoutComponent(panelTwo, TWO);
     
    			//I want it to show the relevant panel when the checkbox gets the focus. 
    			//So I wrote this Focus Adapter.
    			cbTwo.addFocusListener(new FocusAdapter(){
    				@Override
    				public void focusGained(FocusEvent arg0) {
    					System.out.println("Did it change?"); //Testing if 'focus' is what I thought, And it is what I thought.
    					cardLay.show(rightPanel, ONE);		// But it does not show anything on the right side.
    				}
    			});
     
    			cbOne.addFocusListener(new FocusAdapter(){
    				@Override
    				public void focusGained(FocusEvent arg0) {
    					System.out.println("Didn't it change?");
    					cardLay.show(rightPanel, TWO);
    				}
    			});
     
    		}
    	}
    I hope this helps you help me

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: CardLayout does not show my content

    Where do you add anything to the right panel?
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    beer-in-box (March 15th, 2013)

  8. #7
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: CardLayout does not show my content

    Damn...
    It is obvious now of course but... I thought the CardLayout is a little different than others and so I do not need this:
    			rightPanel.add(panelOne);
    			rightPanel.add(panelTwo);
    I mean, I normally do add components to panels (or any other containers), not to layout, but CardLayout sounded a little different...
    OK this was stupid.

    Added the code. It works.

    Thank you.

Similar Threads

  1. [SOLVED] CardLayout show a specific card with button click
    By IanSawyer in forum AWT / Java Swing
    Replies: 2
    Last Post: April 13th, 2012, 11:07 AM
  2. Problem with CardLayout
    By FireStorm in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2012, 03:45 PM
  3. Canvas in JFrame, does not show content
    By Nesh108 in forum Java Theory & Questions
    Replies: 6
    Last Post: October 31st, 2011, 08:58 PM
  4. Beginner: Show Image in Label when Results Show Up
    By Big Bundy in forum Java Theory & Questions
    Replies: 3
    Last Post: April 4th, 2011, 02:43 PM
  5. Cardlayout help
    By phantomswordsmen in forum AWT / Java Swing
    Replies: 1
    Last Post: December 22nd, 2010, 02:36 PM