Results 1 to 12 of 12
  1. #1
    AutoRobin is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default JFrame and CardLayout -- Wrong Parent

    So here is my code. It is three parts, 2 are shown, the last is not relevant:

    Java Code:
    //First file
    import javax.swing.*;
    import java.awt.event.*;
    import java.lang.String;
    import java.lang.Object;
    import java.util.Formatter;
    import java.awt.*;
     
    public class ToDoItBetterAgain extends JFrame
    {
    	//Attributes
    	private CardLayout myCard;
    	private MainPanel mainPanel;
    	private AddPanel addPanel;
        
        public static void main(String[] args) 
        {
        	new ToDoItBetterAgain();
        }
        
        ToDoItBetterAgain()
        {
        	this.setSize(600,400);
        	setVisible(true);
        	
        	//Card Layout
    		myCard = new CardLayout();
    		setLayout(myCard);
    		
    		//Cards
    		mainPanel = new MainPanel(myCard, this);
    		addPanel = new AddPanel(myCard, this);
    		
    		add(mainPanel, "Main");
    		add(addPanel, "Add");
        }
    }

    Java Code:
    //First Panel
    import javax.swing.*;
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.lang.String;
    import java.lang.Object;
    import java.util.Formatter;
    
    class MainPanel extends JPanel
    {
    	private JButton add, apply;
    	private JLabel error, topLabel, bigLabel; //bigLabel - JTextArea?
    	private CardLayout myCard;
    	private JFrame original;
    	
    	public MainPanel(CardLayout myCard, JFrame original)
    	{
    		this.myCard = myCard;
    		this.original = original;
    		
    		//North - topLabel
    		JPanel north = new JPanel();
    		LabelFormat big = new LabelFormat("Name","Date","Time","Remove","Delete");
    		topLabel = new JLabel(big.getActualString());
    		topLabel.setFont(new Font("Times New Roman", Font.PLAIN, 18));
    		topLabel.setForeground(Color.BLUE);
    		north.add(topLabel);
    		
    		//Center - bigLabel
    		bigLabel = new JLabel("");
    		bigLabel.setVerticalAlignment(SwingConstants.TOP);
    		bigLabel.setHorizontalAlignment(SwingConstants.LEFT);
    		bigLabel.setFont(new Font("Times New Roman", Font.PLAIN, 16));
    		bigLabel.setText("No Events");
    		
    		//East - Check Boxes
    		
    		
    		//South - Error label, Add/Apply buttons
    		JPanel south = new JPanel();
    			//Buttons
    			JPanel subsouth1 = new JPanel();
    		
    			add = new JButton("ADD");
    			apply = new JButton("APPLY");
    			subsouth1.setLayout(new FlowLayout(FlowLayout.RIGHT));
    			subsouth1.add(add);
    			subsouth1.add(apply);
    			
    			//Label
    			JPanel subsouth2 = new JPanel();
    			error = new JLabel("");
    			error.setForeground(Color.RED);
    			
    			subsouth2.setLayout(new FlowLayout(FlowLayout.LEFT));
    			subsouth2.add(error);
    			
    		south.setLayout(new GridLayout(1,2));
    		south.add(subsouth2);
    		south.add(subsouth1);
    		
    		
    		//Put it all together
    		setLayout(new BorderLayout());
    		add(north, BorderLayout.NORTH);
    		add(bigLabel, BorderLayout.CENTER);
    		add(south, BorderLayout.SOUTH);
    		
    		
    		//Button Listeners
    		ButtonListener listener = new ButtonListener();
    		apply.addActionListener(listener);
    		add.addActionListener(listener);
    	}
    	
    	private class ButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			if(event.getSource() == add)
    			{
    				myCard.last(original);
    			}
    			else if(event.getSource() == apply)
    			{
    			}
    			else
    			{
    				error.setText("Error");
    			}
    			
    			
    		}
    	}
    }



    The files compile correctly. The file opens and MainPanel loads properly. However, when I hit the "Add" button I get this error:

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: wrong parent for CardLayout
    at java.awt.CardLayout.checkLayout(CardLayout.java:38 4)
    at java.awt.CardLayout.last(CardLayout.java:479)
    at MainPanel$ButtonListener.actionPerformed(MainPanel .java:82)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6289)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:605 4)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4652)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 82)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 82)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:644)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:603)
    at java.awt.EventQueue$1.run(EventQueue.java:601)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:617)
    at java.awt.EventQueue$2.run(EventQueue.java:615)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 614)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

    So I'm guessing I am calling the wrong parent, but I don't know who is the real parent is. I'm guessing this is just a rookie mistake, and hoping.

    Thanks
    Last edited by Fubarable; 04-21-2011 at 05:13 AM. Reason: Mod Edit: code tags added

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You pass the CardLayout to the MainPanel class but you do not use it.

    I'm no Swing expert but I'm not sure you can use the same layout manager for every panel and frame you have.

  3. #3
    AutoRobin is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    @Junky,

    Thanks for the quick reply. The CardLayout gets passed to MainPanel via the Constructor: public MainPanel(CardLayout myCard, JFrame original). Also, I have nearly identical code for a JApplet version and it works there... No wrong parent issue.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by AutoRobin View Post
    @Junky,

    Thanks for the quick reply. The CardLayout gets passed to MainPanel via the Constructor: public MainPanel(CardLayout myCard, JFrame original).
    I know, I read your code but you don't use it.

  5. #5
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    The exception is being thrown at this line inside ButtonListener:
    Java Code:
    myCard.last(original);
    What's weird is that you properly call setLayout(myCard) in the ToDoItBetterAgain class. So it is supposed to be the correct parent......

    EDIT: @Junky, yes he does use it: inside ButtonListener
    EDIT2: @AutoRobin, please use [ code ] code goes here [ /code ] tags next time (without the spaces) so it would be easier to read your code.
    Last edited by ra4king; 04-21-2011 at 04:09 AM.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by ra4king View Post
    EDIT: @Junky, yes he does use it: inside ButtonListener
    I meant OP doesn't use it as the layout manager of the JPanel.

  7. #7
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    But he needs reference to that object for ButtonListener....

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    So? It doesn't change the fact that the JPanel's layout manager IS NOT the CardLayout.

  9. #9
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    If you look closer at that 1 line:
    Java Code:
    myCard.last(original);
    "original" is referencing the JFrame that has a layout of the instance referenced by "myCard".

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Moderator Edit: Code tags added to the post above.

    To the OP, in the future, to do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Best of luck

    ________________________________________________

    Regarding your problem, I think that the issue is that the layout is not added to the JFrame but rather to its contentPane. Myself, I'd give the main class that has the CardLayout a public method, say

    Java Code:
    public void swapCard(String cardName) {
        myCard.show(getContentPane(), cardName);
    }
    And I'd have my sub panels call this method passing in the appropriate String. I'd make the cardName Strings public constants in the main class, so there's less risk of a spelling or capitalization mistake, and I'd have my sub panel classes use these constants when calling this method.

    Best of luck

    Best of luck

  11. #11
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    @Fubarable
    Good point! He should probably call "myCard.last(original.getContentPane())"!

  12. #12
    AutoRobin is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    @Furable, @ra4king,

    Thanks for the advice! It works. I did need to call myCard.show(original.getcontentpane(),"Add").

Similar Threads

  1. Question about cardlayout
    By scheffetz in forum New To Java
    Replies: 1
    Last Post: 04-02-2011, 08:05 PM
  2. CardLayout manager
    By abetemari in forum New To Java
    Replies: 4
    Last Post: 03-27-2011, 01:46 AM
  3. Using a FlowLayout on top of a CardLayout
    By snieuw in forum New To Java
    Replies: 1
    Last Post: 11-08-2010, 06:03 PM
  4. Help with CardLayout
    By Kyle227 in forum New To Java
    Replies: 4
    Last Post: 05-28-2010, 01:03 AM
  5. Regarding CardLayout
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-07-2008, 07:52 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
  •