Results 1 to 18 of 18
  1. #1
    Angelo is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default CardLayout with JPanels

    Hi, I'm new with Java... I'm trying to create a simple program with a button which allows to switch between two different background images. I guess I have a problem during the runtime. Here is the error that i obtained:
    Exception in Thread "main": java.lang.IllegalArgumentException: wrong parent for CardLayout
    at java.awt.CardLayout.checkLayout(CardLayout.java:38 4)
    at java.awt.CardLayout.show(CardLayout.java:506)
    at SetupSfondi.ImpostaSfondi(setupSfondi.java: 41)
    at SetupSfondi.main(setupSfondi.java:57)


    Here is the code I'm running:


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class SetupSfondi {
        private JPanel p1, p2;
        private JFrame f;
        static private JButton button;
        private CardLayout cardLayout;
        String [] path = {"c:\\users\\deliana\\desktop\\rihanna.jpg","c:\\users\\deliana\\desktop\\tavolo.jpg"};
        public SetupSfondi() {
            f = new JFrame ("CardLayout");
            button = new JButton ("Cambia Sfondo");
            cardLayout = new CardLayout();
            final ImageIcon [] icon = {new ImageIcon (path[0]), new ImageIcon(path[1])}; 
            p1 = new JPanel()
            {
                protected void paintComponent (Graphics g)
                {
                    Dimension d = getSize();
                    g.drawImage(icon[0].getImage(),0,0,d.width,d.height,null);
                    super.paintComponent(g);
                }
            };
            p2 = new JPanel()
            {
                protected void paintComponent (Graphics g)
                {
                    Dimension d = getSize();
                    g.drawImage(icon[1].getImage(),0,0,d.width,d.height,null);
                    super.paintComponent(g);
                }
            };
        };
            
        public void ImpostaSfondi() {
            f.setLayout(cardLayout);
            f.getContentPane().add(p1, "uno");
            f.getContentPane().add(p2, "due");
            p1.add(button);
            cardLayout.show(f,"uno");
            f.setSize (400,400);
            f.setVisible(true);
        };
            
        public void cambiaSfondo() {
         //   try {
                cardLayout.next (f);
           // }
           // catch (InterruptedException exc) {
           //     exc.printStackTrace ();
            //}
        };
                
        public static void main (String [] args) {
            SetupSfondi setupSfondi = new SetupSfondi();
            setupSfondi.ImpostaSfondi();
            DelegationModel dm = new DelegationModel();
            dm.setup(button);
        }
    };

    Does someone can help me???
    Last edited by KevinWorkman; 12-15-2011 at 03:53 PM. Reason: added code tags

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default Re: CardLayout with JPanels

    When posting code, please don't forget the CODE tags. I've added them for you this time, but please be more mindful in the future.

    When you call things like JFrame.add() and JFrame.setLayout(), that's actually being passed to its content pane, a JPanel, as specified in the API: JFrame (Java Platform SE 6)

    That means that the CardLayout's parent is the JFrame's content pane, not the JFrame itself.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Angelo is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: CardLayout with JPanels

    So what am I doing? Am I setting a Layout to the JPanel instead of setting it for the JFrame? So how can I show different JPanels?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default Re: CardLayout with JPanels

    You are probably doing everything fine, except the line that throws the error. You're using the JFrame when you should be using the JFrame's content pane.

    Recommended reading: How to Use CardLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Angelo is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: CardLayout with JPanels

    Uh... Just now I got a result... I changed the argument of show method from f to f.getContentPane. I guess this is what you were suggesting right?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default Re: CardLayout with JPanels

    Quote Originally Posted by Angelo View Post
    Uh... Just now I got a result... I changed the argument of show method from f to f.getContentPane. I guess this is what you were suggesting right?
    Yep! Does it work as expected now?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Angelo is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: CardLayout with JPanels

    No.... now I have a mess!!! So many exceptions.... Basically now I have the frame with the button but no background... No RIHANNA. Then when I press the button I have a lot of excpetions. Now as you can see i commented the lines in function CambiaSfondo(). That's because when I compile I have an error which is:
    exception java.lang.InterruptedException is never thrown in body of corresponding try statement
    catch (InterruptedException exc) {

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default Re: CardLayout with JPanels

    What exceptions are you seeing? I don't see a place where you add an ActionListener to the JButotn, so I'm not sure how that can cause any output at all.

    And that problem is because you're catching an Exception that the line of code in the try block will never throw. Why are you doing that?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: CardLayout with JPanels

    The tutorial has a working example. Compare your code with the tutorial to see what you are doing differently.

  10. #10
    Angelo is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: CardLayout with JPanels

    I can't find the code tags to post my code... where are?
    [FONT=Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace]
    [/FONT]

  11. #11
    Angelo is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: CardLayout with JPanels

    [FONT=Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace]import java.swing.*;
    [/FONT]

  12. #12
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: CardLayout with JPanels

    I can't find the code tags to post my code... where are?
    Yes, I reported this a couple of months ago. See: Missing Markup Buttons. Maybe add your own comment that the code tags are still missing and they will look into it again.

    In the meantime the code tag is just like the font tag above except you use "code" instead of "font" inside the tag.

  13. #13
    Angelo is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: CardLayout with JPanels

    ok i try...
    so this is the DelegationModel method:
    [code]import java.awt.*;
    import javax.swing.*;

    public class DelegationModel {
    public void setup(JButton b) {
    b.addActionListener (new ButtonHandler());
    }
    }[\code]

    and here is the ButtonHandler:
    [code]import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class ButtonHandler implements ActionListener {
    public void actionPerformed (ActionEvent e) {
    SetupSfondi sf = new SetupSfondi();
    sf.cambiaSfondo();
    }
    }[\code]

    the exception is always a IllegalArgument exception and it is related to the cambiaSfondo method in SetupSfondi class

  14. #14
    Angelo is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: CardLayout with JPanels

    I tried... I failed...

  15. #15
    Angelo is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: CardLayout with JPanels

    ok I added a line when I create the JPanels which is setOpaque(false) and now the first background is visible, but I still have a lot of problems when i press the button... Here is a screenshot of what happens when I execute my programCardLayout with JPanels-screenshot.jpg
    while this is what happens when i press the button "cambia sfondo"CardLayout with JPanels-screenshot2.jpg

    what am I doing wrong????

  16. #16
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default Re: CardLayout with JPanels

    Are you sure you're running the code you posted above? It looks like you're trying to access an array index of 2, which I don't see in your original code.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  17. #17
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: CardLayout with JPanels

    Use a forward slash!!!

    You have working code. Compare the working code to your code. The working code uses a combo box to swap panels. You are using a button. The the code is almost identical because both components respond to an event. So all you have to do is specify the proper card you want to be displayed.

  18. #18
    Angelo is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: CardLayout with JPanels

    ok now I fixed the code and I have another exception:
    IllegalArgumentException: wrong Parent exception for CardLayout
    the problem is in the function cambiaSfondo().

    I think I'm getting the point but I don't know if I'm on the right way: when I press the button the action is to call a new SetupSfondi function, so the machine repeats all the instructions and not only the cambiaSfondo method. Do you think this could be the problem?

    If you see the ButtonHandler class I create a new object called sf...

    So if this is the problem how can I call a method in class SetupSfondi from outside that class?

Similar Threads

  1. CardLayout Help
    By David M. in forum New To Java
    Replies: 4
    Last Post: 08-06-2011, 02:57 AM
  2. Replies: 2
    Last Post: 04-26-2011, 02:50 PM
  3. Problems regarding JPanels in JPanels
    By ColtonPhillips in forum AWT / Swing
    Replies: 2
    Last Post: 07-19-2010, 08:33 PM
  4. Populating CardLayout JPanels with External classes
    By fred2482 in forum New To Java
    Replies: 5
    Last Post: 12-21-2009, 06:08 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
  •