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

Thread: CardLayout show a specific card with button click

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default CardLayout show a specific card with button click

    I'm having trouble with the CardLayout to have a particular card to be displayed when a button is clicked.

    My code is posted below and I am sure that the issue is with the ".show" part but I am not sure what is exactly wrong with it as I am a complete novice with Java and Java Swing so I am pretty much learning as I go!

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class CardLayoutTest {
     
        public static void main(String[] args) {
     
            final String card1Text = "Card 1";
            final String card2Text = "Card 2";
            final String card3Text = "Card 3";
            final JPanel cards; //a panel that uses CardLayout
            // button commands
            final String ONE = "ONE";
            final String TWO = "TWO";
            final String THREE = "THREE";
     
     
            JFrame frame = new JFrame("CardLayout Test");
     
     
            //Create the "cards".
            JPanel card1 = new JPanel();
            card1.setBackground(Color.RED);
     
            JPanel card2 = new JPanel();
            card2.setBackground(Color.GREEN);
     
            JPanel card3 = new JPanel();
            card3.setBackground(Color.BLUE);
     
            //Create the panel that contains the "cards".
            cards = new JPanel(new CardLayout());
            cards.add(card1, card1Text);
            cards.add(card2, card2Text);
            cards.add(card3, card3Text);
     
     
            class ControlActionListenter implements ActionListener {
                public void actionPerformed(ActionEvent e) {
                    CardLayout cl = (CardLayout) (cards.getLayout());
                    String cmd = e.getActionCommand();
                    if (cmd.equals(ONE)) {
                        cl.show(cards, ONE);
                    } else if (cmd.equals(TWO)) {
                        cl.show(cards, TWO);
                    } else if (cmd.equals(THREE)) {
                        cl.show(cards, THREE);
                    }
                }
            }
            ControlActionListenter cal = new ControlActionListenter();
     
            JButton btn1 = new JButton("ONE");
            btn1.setActionCommand(ONE);
            btn1.addActionListener(cal);
     
            JButton btn2 = new JButton("TWO");
            btn2.setActionCommand(TWO);
            btn2.addActionListener(cal);
     
            JButton btn3 = new JButton("THREE");
            btn3.setActionCommand(THREE);
            btn3.addActionListener(cal);
     
            JPanel controlButtons = new JPanel();
            controlButtons.add(btn1);
            controlButtons.add(btn2);
            controlButtons.add(btn3);
     
     
            Container pane = frame.getContentPane();
            pane.add(cards, BorderLayout.CENTER);
            pane.add(controlButtons, BorderLayout.PAGE_END);
     
     
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300, 200);
            frame.setVisible(true);
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: CardLayout show a specific card with button click

    Pass the same argument name to the show method as you did to the add method.

  3. The Following User Says Thank You to copeg For This Useful Post:

    IanSawyer (April 13th, 2012)

  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: CardLayout show a specific card with button click

    Thanks for your help!

    It works as intended now!

Similar Threads

  1. CardLayout wont go to the next card
    By pottsiex5 in forum AWT / Java Swing
    Replies: 2
    Last Post: November 25th, 2011, 04:04 PM
  2. [SOLVED] .show is called in card layout, but the frame didn't update
    By nggdowt in forum AWT / Java Swing
    Replies: 2
    Last Post: November 9th, 2011, 02:16 AM
  3. add JTextField with a click of button
    By A4Andy in forum AWT / Java Swing
    Replies: 1
    Last Post: August 31st, 2011, 07:34 AM
  4. Lock up code to the click of a button!!!
    By Allen Walker in forum Java Theory & Questions
    Replies: 2
    Last Post: July 1st, 2011, 09:12 AM
  5. close JDialog on button click
    By Christophe in forum AWT / Java Swing
    Replies: 4
    Last Post: April 4th, 2010, 11:04 PM