Search code examples
javaswingactionlistenercardlayout

GUI Card Layout- Action Listener not working


I only have the first button set up to work. It was working fine until I updated panel one to have additional panels (oneTwo,oneThree, etc.). I did this because I have to make the program look like this => Example

but now when i click the first button it no longer switched to card two. I cant figure it out.

Here is my code

public class AddressBook implements ActionListener {
    static CardLayout contentPaneLayout;
    static JButton B1= new JButton("Create a New Address"),  B2= new JButton("Load Contacts"), B3= new JButton("Add New Contacts"),
            B4= new JButton("View/Delete Contacts"), B5= new JButton("Backup Contacts"),  B6= new JButton("Exit"), B7 = new JButton ("Cancel");
    static JTextField TF1 = new JTextField(13), TF2 = new JTextField (13);
    static JLabel L1 = new JLabel ("Use The Buttons Below To Manage Contacts",JLabel.CENTER), L2 = new JLabel ("This is Card 2"), L3 = new JLabel("Username:",JLabel.CENTER),
            L4 = new JLabel("Number of Contacts:",JLabel.CENTER);;
    static Container contentPane;


    public void actionPerformed(ActionEvent e) {
        Object click = e.getSource();
        if (click==B1) contentPaneLayout.show(contentPane,"Card 2");

        if (click==B6) System.exit(0);
        if (click==B7) contentPaneLayout.show(contentPane, "Card 1");




    }

    public static void main(String[] args) {
        JFrame frm = new JFrame("Address Book");
        contentPane = frm.getContentPane();
        contentPane.setLayout(new CardLayout());
        JPanel one = new JPanel (new GridLayout( 5,5 ));
        JPanel two = new JPanel (new GridLayout(4,6));

        JPanel oneTwo = new JPanel(new FlowLayout());
        JPanel oneThree = new JPanel(new GridLayout(2,3,6,5));
        JPanel oneFour = new JPanel(new GridLayout());
        JPanel oneFive = new JPanel(new GridLayout());




        one.add (L1);
        oneTwo.add (L3);
        oneTwo.add (TF1);
        TF1.setEditable(false);
        TF2.setEditable(false);
        oneTwo.add(L4);
        oneTwo.add(TF2);


        one.add(oneFive);
        one.add(oneTwo);
        oneThree.add(B1); oneThree.add(B2);oneThree.add(B3);oneThree.add(B4);oneThree.add(B5);oneThree.add(B6);
        one.add(oneFour);
        one.add(oneThree);

        two.add(L2);
        two.add(B7);

        contentPane.add("Card 1", one);
        contentPane.add("Card 2", two) ;
        //contentPaneLayout.show(contentPane, "Card 1");



        ActionListener AL= new AddressBook();
        B1.addActionListener(AL); B7.addActionListener(AL); B6.addActionListener(AL);
        frm.pack();
        frm.setSize(550,250);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setVisible(true);
    }

}

Solution

  • You have a NullPointerException

    contentPaneLayout.show(contentPane, "Card 2");
    

    contentPaneLayout is never assigned a value.

    This is exacerbated by the over zealous use of static

    A little bit of rework and you have a slightly better solution

    import java.awt.CardLayout;
    import java.awt.Container;
    import java.awt.EventQueue;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class AddressBook implements ActionListener {
    
        private CardLayout contentPaneLayout;
        private JButton b1 = new JButton("Create a New Address"), b2 = new JButton("Load Contacts"), b3 = new JButton("Add New Contacts"),
                b4 = new JButton("View/Delete Contacts"), b5 = new JButton("Backup Contacts"), b6 = new JButton("Exit"), b7 = new JButton("Cancel");
        private JTextField tf1 = new JTextField(13), tf2 = new JTextField(13);
        private JLabel l1 = new JLabel("Use The Buttons Below To Manage Contacts", JLabel.CENTER), l2 = new JLabel("This is Card 2"), l3 = new JLabel("Username:", JLabel.CENTER),
                l4 = new JLabel("Number of Contacts:", JLabel.CENTER);
    
        private Container contentPane;
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new AddressBook();
                }
            });
        }
    
        public AddressBook() {
            JFrame frm = new JFrame("Address Book");
            contentPane = frm.getContentPane();
            contentPaneLayout = new CardLayout();
            contentPane.setLayout(contentPaneLayout);
            JPanel one = new JPanel(new GridLayout(5, 5));
            JPanel two = new JPanel(new GridLayout(4, 6));
    
            JPanel oneTwo = new JPanel(new FlowLayout());
            JPanel oneThree = new JPanel(new GridLayout(2, 3, 6, 5));
            JPanel oneFour = new JPanel(new GridLayout());
            JPanel oneFive = new JPanel(new GridLayout());
    
            one.add(l1);
            oneTwo.add(l3);
            oneTwo.add(tf1);
            tf1.setEditable(false);
            tf2.setEditable(false);
            oneTwo.add(l4);
            oneTwo.add(tf2);
    
            one.add(oneFive);
            one.add(oneTwo);
            oneThree.add(b1);
            oneThree.add(b2);
            oneThree.add(b3);
            oneThree.add(b4);
            oneThree.add(b5);
            oneThree.add(b6);
            one.add(oneFour);
            one.add(oneThree);
    
            two.add(l2);
            two.add(b7);
    
            contentPane.add("Card 1", one);
            contentPane.add("Card 2", two);
            //contentPaneLayout.show(contentPane, "Card 1");
    
            b1.addActionListener(this);
            b7.addActionListener(this);
            b6.addActionListener(this);
            frm.pack();
            frm.setSize(550, 250);
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frm.setVisible(true);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            Object click = e.getSource();
            System.out.println("...");
            if (click == b1) {
                contentPaneLayout.show(contentPane, "Card 2");
            }
    
            if (click == b6) {
                System.exit(0);
            }
            if (click == b7) {
                contentPaneLayout.show(contentPane, "Card 1");
            }
    
        }
    
    }
    

    The important change is in the constructor...

    contentPaneLayout = new CardLayout();
    contentPane.setLayout(contentPaneLayout);