Search code examples
javaswingcardlayout

CardLayout to close on last card


So I have a JPanel with a CardLayout. this CardLayout, as expected, manages the switching of panels in the frame. The switching is done by two buttons: "Back" and "Next".

I want to know if there is a way to close the whole application (i.e. call System.exit(0)) when it is on the last card and "Next" is pressed again.

I have looked for a solution everywhere, but I can't find anything.

The problem is: I don't know how to check which is the last one.

Here is the listener excerpt of my code:

public void actionPerformed(ActionEvent arg0) {
        CardLayout l = (CardLayout) holder.getLayout();
        if(arg0.getSource() == opt[1]){ //opt[1] is the "Next" button


                //Insert if statement here to check if
                //the CardLayout is on the last card
                {
                System.exit(0);
                } else {
                    l.next(holder); //holder is the JPanel with the CardLayout
                }
        }
}

Solution

  • What about dispose() which is inherited from Window? Make sure you set:

    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    

    JFrame frame = ...
    
    // ...
    
    frame.setVisible(false); // hide the GUI
    frame.dispose(); // destroy and release the GUI resources
    

    For example:

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class CardLayoutGUI
    {
        private JFrame frame;
        private JButton btnBack;
        private JButton btnNext;
        private CardLayout cLayout;
        private JPanel panUp;
        private JPanel panDown;
    
        private static final String[] cards =
        {"card1", "card2", "card3", "card4", "card5"}; 
    
        private int currentCard = 0;
    
        public void init()
        {
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            ((JPanel)frame.getContentPane()).setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    
            btnBack = new JButton("Back");
            btnNext = new JButton("Next");
    
            btnBack.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    btnNext.setText("Next");
                    currentCard--;
                    cLayout.show(panUp, cards[currentCard]);
                    if(currentCard == 0) btnBack.setVisible(false);
                }
            });
    
            btnNext.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    btnBack.setVisible(true);
                    currentCard++;
    
                    if(currentCard == cards.length - 1) // last card
                    {
                        btnNext.setText("Exit");
                        cLayout.show(panUp, cards[currentCard]);
                    }
                    else if(currentCard >= cards.length)
                    {
                        frame.setVisible(false);
                        frame.dispose();
                    }
                    else
                    {
                        cLayout.show(panUp, cards[currentCard]);
                    }
                }
            });
    
    
            cLayout = new CardLayout();
            panUp = new JPanel(cLayout);
            panDown = new JPanel();
            frame.add(panUp, BorderLayout.CENTER);
            frame.add(panDown, BorderLayout.SOUTH);
            panDown.add(btnBack);
            panDown.add(btnNext);
    
            for(int i = 0; i < cards.length; i++) createPanels(panUp, cards[i]);
    
            frame.pack();
            frame.setLocationRelativeTo(null);
    
            btnBack.setVisible(false);
        }
    
        public void showGUI()
        {
            frame.setVisible(true);
        }
    
        private void createPanels(JPanel container, String label)
        {
            JPanel pan = new JPanel();
            pan.add(new JLabel(label));
            container.add(pan, label);
        }
    
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    CardLayoutGUI clg = new CardLayoutGUI();
                    clg.init();
                    clg.showGUI();
                }
            });
        }
    }