title: Implementing back/forward buttons in Swing
body: <p>I have a quick question. </p>
  
  <p>I'm getting a little bit of experience with Swing and the easiest way to do this was to draw up a reasonably big GUI.</p>
  
  <p>As part of the GUI, I want to have Forward and Back Buttons. The Approach I'm trying to take is to implement methods that will push the current JPanel to a stack and retrieve the previous value (Be that in a forwards or reverse direction (hence 2 stacks)). I can't get it to work though. Perhaps I'm going about it completely the wrong way or maybe a stack can't be used int the way I'm using it. In either case, it's really bugging me. I imagine there are probably easier ways like a card layout but I think this approach should work and that's what's so annoying.</p>
  
  <p>It may be worth noting that I'm using a JFrame "base class" and changing the central JPanel depending on the screen. The nav bar is constant as a part of the "base class" however</p>
  
  <p>The code of this "base class":</p>
  
  <pre><code>public class Main_Frame extends JFrame{
      static JPanel nav_bar_panel;
      JButton home;
      JButton back;
      JButton forward;
      JPanel currentPanel;
  
      static Stack&lt;JPanel&gt; previousPanels;
      static Stack&lt;JPanel&gt; forwardPanels;
  
      public Main_Frame(){
          super("DEMO");
          setSize(800,600);
          setLayout(new BorderLayout());
          setVisible(true);
  
          add(nav_bar(), BorderLayout.NORTH);
          currentPanel = init_display();
          add(currentPanel, BorderLayout.CENTER);
  
          previousPanels = new Stack&lt;JPanel&gt;();
          forwardPanels  = new Stack&lt;JPanel&gt;();
      }
  
      private JPanel nav_bar(){
          ButtonPressHandler handler = new ButtonPressHandler();
  
          nav_bar_panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
          back = new JButton("Back");
          back.addActionListener(handler);
          home = new JButton("Home");
          home.addActionListener(handler);
          forward = new JButton("Forward");
          forward.addActionListener(handler);
  
          nav_bar_panel.add(back);
          nav_bar_panel.add(home);
          nav_bar_panel.add(forward);
  
          return nav_bar_panel;
      }
  
      private JPanel init_display(){
          Home_Panel home_panel = new Home_Panel();
  
          return home_panel;
      }
  
      public void change_display(JPanel myPanel){
          invalidate();
          remove(currentPanel);
          previousPanels.push(currentPanel);
          currentPanel = myPanel;
          add(currentPanel);
          validate();
      }
  
      public void previous_display(){
          if(!previousPanels.empty()){
              invalidate();
              remove(currentPanel);
              forwardPanels.push(currentPanel);
              currentPanel = previousPanels.pop();
              add(currentPanel);
              validate();
          }
      }
  
      public void forward_display(){
          if(!forwardPanels.empty()){
              invalidate();
              remove(currentPanel);
              previousPanels.push(currentPanel);
              currentPanel = forwardPanels.pop();
              add(currentPanel);
              validate();
          }
      }
  
      private class ButtonPressHandler implements ActionListener 
         {
            public void actionPerformed( ActionEvent event )
            {
                if(event.getSource() == back){
                    previous_display();
                    System.out.print("You selected back");
                } else if(event.getSource() == forward){
                    forward_display();
                    System.out.print("You selected forward");
                }
            } // end method actionPerformed
         } // end private inner class TextFieldHandler
  
  }
  </code></pre>