0

I'm trying to implement a back button in my GUI which goes to the previous card using an action listener and running the code

cards.previous(contentPane);

when the button is pressed.

My problem is that after clicking the back button, no matter which button I press on the GUI, it always goes to the card that I came from. So the numbers being the JPanels

2 --back--> 1 --*--> 2

back and * (=anything) are the buttons that I press. Why does this happen?

EDIT: App extends JFrame

public App() {
    cards = new CardLayout();
    contentPane = new JPanel();
    contentPane.setLayout(cards);
    contentPane.add(new StartPanel(this));
}

public void next(JPanel p) {
    contentPane.add(p);
    cards.next(contentPane);
}

public void prev() {
    cards.previous(contentPane);
}

StartPanel has

public StartPanel(App app) {
    JButton btn2 = new JButton("2");
    JButton btn3 = new JButton("3");
    btn2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            app.next(new JPanel());
        }
    });
    btn3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            app.next(new JPanel());
        }
    });
    add(btn2);
    add(btn3);
}

My problem is that when I go from panel 2 to panel 1(StartPanel) by pressing the back button, then press btn3, it goes to panel 2 instead of panel 3.

absolutelydevastated
  • 1,657
  • 1
  • 11
  • 28

0 Answers0