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 10 of 10

Thread: swing cardlayout(want to create several cards using panel but failed plz help)

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default swing cardlayout(want to create several cards using panel but failed plz help)

    package swing1;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class card1
    {
       JFrame f;
       JButton b1,b2;
       JPanel p,p1,p2,p3;
       JLabel l1,l2,l3;
     
       card1()
       {
           f=new JFrame("testing card layout");
           f.setSize(400,400);
           f.setLayout(null);
           f.setBackground(Color.red);
     
           b1=new JButton("prev");
           b1.setBounds(90,30,70,30);
           b2=new JButton("next");
           b2.setBounds(170,30,70,30);
     
           l1=new JLabel("I m on CARD 1");
           l2=new JLabel("I m on CARD 2");
           l3=new JLabel("I m on CARD 3");
     
           p=new JPanel(new CardLayout());
           p.setBackground(Color.red);
     
           p1=new JPanel(new FlowLayout());
           p2=new JPanel(new FlowLayout());
           p3=new JPanel(new FlowLayout());
           //p=(panel).add(f.getContentPane());
     
           f.add(p);
           p.add(l1,"");
           f.add(b1);
           f.add(b2);
           p.add(p1,"1");
           p.add(p2,"2");
           p.add(p3,"3");
           f.setVisible(true);
     
           process pp=new process();
           b1.addActionListener(pp);
           b2.addActionListener(pp);
       }
     
       class process implements ActionListener
       {
           public void  actionPerformed(ActionEvent e)
           {
               if(e.getSource()==b2)
               {
                   new CardLayout().show(p1, null);
               }
               else
               {
                   new CardLayout().show(p2, null);
               }
           }
       }
     
       public static  void main(String args[])
       {
     
           card1 cd=new card1();
       }
     
    }


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

    Default Re: swing cardlayout(want to create several cards using panel but failed plz help)

    Please explain what the problem is. If there are errors copy the full text of the message and post it here.
    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: swing cardlayout(want to create several cards using panel but failed plz help)

    sir actually I am unable to understand the concept of card layout
    whether a frame should have card layout or a panel should have card layout and how it works
    and in the above code i tried according to my understanding but it is not displaying result so plz help me....
    I tried a lot and stuck on this topic from last 2 days

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

    Default Re: swing cardlayout(want to create several cards using panel but failed plz help)

    am unable to understand the concept of card layout
    Have you seen the tutorial? It has an example you could copy and work with.
    How to Use CardLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    it is not displaying result
    What does it display when you compile and execute it?

    Are there any errors? Please copy and paste text here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: swing cardlayout(want to create several cards using panel but failed plz help)

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class CardLayoutDemo implements ItemListener {
        JPanel cards; //a panel that uses CardLayout
        final static String BUTTONPANEL = "Card with JButtons";
        final static String TEXTPANEL = "Card with JTextField";
     
        public void addComponentToPane(Container pane) {
            //Put the JComboBox in a JPanel to get a nicer look.
            JPanel comboBoxPane = new JPanel(); //use FlowLayout
            String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };
            JComboBox cb = new JComboBox(comboBoxItems);
            cb.setEditable(false);
            cb.addItemListener(this);
            comboBoxPane.add(cb);
     
            //Create the "cards".
            JPanel card1 = new JPanel();
            card1.add(new JButton("Button 1"));
            card1.add(new JButton("Button 2"));
            card1.add(new JButton("Button 3"));
     
            JPanel card2 = new JPanel();
            card2.add(new JTextField("TextField", 20));
     
            //Create the panel that contains the "cards".
            [COLOR="#008000"]cards = new JPanel(new CardLayout());
            cards.add(card1, BUTTONPANEL);
            cards.add(card2, TEXTPANEL);[/COLOR]
     
            pane.add(comboBoxPane, BorderLayout.PAGE_START);
            pane.add(cards, BorderLayout.CENTER);
        }
     
        public void itemStateChanged(ItemEvent evt) {
            [COLOR="#008000"]CardLayout cl = (CardLayout)(cards.getLayout());
            cl.show(cards, (String)evt.getItem());[/COLOR]
        }
     
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event dispatch thread.
         */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("CardLayoutDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Create and set up the content pane.
            CardLayoutDemo demo = new CardLayoutDemo();
          [COLOR="#008000"]  demo.addComponentToPane(frame.getContentPane());[/COLOR]
     
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            /* Use an appropriate Look and Feel */
            try {
                //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InstantiationException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            /* Turn off metal's use of bold fonts */
            UIManager.put("swing.boldMetal", Boolean.FALSE);
     
            //Schedule a job for the event dispatch thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }


    --- Update ---

    sir plz explain the text that is in green and what is the role of getContentPane()
    Last edited by Norm; December 1st, 2012 at 01:40 PM. Reason: Removed space from code tag

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

    Default Re: swing cardlayout(want to create several cards using panel but failed plz help)

    Did you read the tutorial at the link I posted? It has examples of using CardLayout.

    sir plz explain the text that is in green and what is the role of getContentPane()
    Read the API doc for the class and method for an explanation of the methods. It should all be there
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: swing cardlayout(want to create several cards using panel but failed plz help)

    and this is very humble request to u if possible correct my code that i posted earlier....

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: swing cardlayout(want to create several cards using panel but failed plz help)

    I'd rather you learn how to solve your programming problems, so I'll let you read the tutorial and work on your problem.

    Does the code give any errors when executed?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: swing cardlayout(want to create several cards using panel but failed plz help)

    when i execute this program A frame wid two buttons appear but when i click on next button this exception is displayed as output...

    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.show(CardLayout.java:506)
    at swing1.card1$process.actionPerformed(card1.java:57 )
    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:6288)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:605 3)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4651)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 81)
    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 81)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:643)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:602)
    at java.awt.EventQueue$1.run(EventQueue.java:600)
    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:616)
    at java.awt.EventQueue$2.run(EventQueue.java:614)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 613)
    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)
    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.show(CardLayout.java:506)
    at swing1.card1$process.actionPerformed(card1.java:61 )
    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:6288)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:605 3)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4651)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 81)
    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 81)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:643)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:602)
    at java.awt.EventQueue$1.run(EventQueue.java:600)
    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:616)
    at java.awt.EventQueue$2.run(EventQueue.java:614)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 613)
    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)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: swing cardlayout(want to create several cards using panel but failed plz help)

    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.show(CardLayout.java:506)
    at swing1.card1$process.actionPerformed(card1.java:57 )
    Look at the code on line 57 and see what it is trying to do.
    Then look at the code in the tutorial to see how it is done there.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Simple swing GUI code....Plz help
    By Arati2512 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 13th, 2012, 11:13 AM
  2. Need help with Deck of Cards
    By yanksin1st in forum Java Theory & Questions
    Replies: 1
    Last Post: March 1st, 2012, 01:28 PM
  3. A deck of cards
    By glebovg in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 1st, 2012, 09:46 AM
  4. How to create a custom Swing Component?
    By Pahulja7 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 25th, 2011, 10:55 AM
  5. How to create jtree which takes data from database in swing??
    By neerajshah84 in forum AWT / Java Swing
    Replies: 1
    Last Post: August 4th, 2011, 08:57 AM