title: Java: Reading an array of a directory to display the images contained in the directory to be able to go forwards and backwards through the array
body: <p>Hello and thanks for your assistance in advance.</p>
  
  <p>My goal is to load a directory called resource to create an array. After doing that the program should use a "Next" and "Previous" button to cycle both forwards and backwards through the array by using a counter. Cycling through the array should display the appropriate picture (without knowing what the picture is, just the order that it is in the array).</p>
  
  <p>My code this far:</p>
  
  <pre><code>import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JLabel;
  import javax.swing.JButton;
  import javax.swing.BoxLayout;
  import javax.swing.JTextField;
  import java.io.IOException;
  import javax.swing.ImageIcon;    
  import java.io.*;
  import java.util.*;
  import java.nio.file.Files;
  import java.nio.file.Paths;        
  import java.awt.*;
  import java.awt.event.*;
  
  public class viewer extends JFrame implements ActionListener
  {
      int counter = 0; //Initial counter value
      int maxItems = 0; // Set it equal to the length of the array
      int minItems = 0; //Minimum items for previous button
  
      JPanel botPanel = null;
      JPanel midPanel = null;
      JLabel midLabel = null;
  
      ImageIcon image;
  
      String[] picture = new String [1000];
  
      public viewer()
      {
          setTitle ("Roberts Viewer");
          setSize (1000, 1000);
          setLocationRelativeTo(null);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Allow the X button to close the program and not just the frame
          JPanel midPanel = new JPanel ();
          JPanel botPanel = new JPanel ();
  
          midLabel = new JLabel();
  
  
          JButton prv = new JButton("Previous"); botPanel.add(prv); prv.addActionListener(this);
          JButton nxt = new JButton("Next"); botPanel.add(nxt); nxt.addActionListener(this);
          JButton quitButton = new JButton ("Quit Program"); botPanel.add(quitButton); quitButton.addActionListener(this);
  
          add(midPanel, BorderLayout.CENTER);
          add(botPanel, BorderLayout.SOUTH);
  
          File dir = new File ("resource");
          File[] picture = dir.listFiles();
  
          for (int maxItems = 0;maxItems &lt; picture.length; maxItems++);
  
          midPanel.add(midLabel);
  
          setVisible(true);
      }   
  
      public static void main(String[] args)
      {
          viewer v = new viewer();
      }
  
      public void actionPerformed (ActionEvent e) 
      {
          String action = e.getActionCommand();
  
          if (action.equals("Quit Program"))
          {
              System.exit(0);
          }
  
          if(action.equals("Next"))
          {
              if (counter &lt; 0){counter = 0;}
              if (counter&gt; maxItems) {counter = 0;}
  
              String pictureString = String.format("resource/%s", picture[counter]);
              System.out.printf("Retrieving[%s]\n",pictureString);
  
              try
              {
                  image = new ImageIcon(getClass().getClassLoader().getResource(pictureString));
              }
              catch (Exception xu)
              {
                  System.out.println("Woops");
              }
  
              midLabel.setIcon(image);
              counter++;
          }
  
          if(action.equals("Previous"))
          {
              if (counter &lt; 0) {counter = maxItems;}
              if (counter &lt; minItems) {counter = maxItems;}
  
              String pictureString = String.format("resource/%s",picture[counter]);
              System.out.printf("Retrieving[%s]\n", pictureString);
  
              try
              {
                  image = new ImageIcon(getClass().getClassLoader().getResource(pictureString));
              }
              catch (Exception xu)
              {
                  System.out.println("Woops");
              }
  
              midLabel.setIcon(image);
              counter--;
          }
      }
  }
  </code></pre>