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 -


hello , assistance in advance.

my goal load directory called resource create array. after doing program should use "next" , "previous" button cycle both forwards , backwards through array using counter. cycling through array should display appropriate picture (without knowing picture is, order in array).

my code far:

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 equal length of array     int minitems = 0; //minimum items 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 x button close program , not 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();          (int maxitems = 0;maxitems < 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 < 0){counter = 0;}             if (counter> 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 < 0) {counter = maxitems;}             if (counter < 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--;         }     } } 

the problem solved. had bugs here fixed 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 equal length of array         int minitems = 0; //minimum items previous button          jpanel botpanel = null;         jpanel midpanel = null;         jlabel midlabel = null;           imageicon image;          file[] picture;           public viewer()         {             settitle ("roberts viewer");             setsize (500, 500);             setlocationrelativeto(null);             setdefaultcloseoperation(jframe.exit_on_close);             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");             picture = dir.listfiles();             midpanel.add(midlabel);             image = new imageicon("resource/"+picture [0].getname());             midlabel.seticon(image);             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"))             {                 counter++;                 if (counter>= picture.length) {counter = 0;}                  try                 {                     image = new imageicon("resource/"+picture[counter].getname());                     midlabel.seticon(image);                  }                 catch (exception xu)                 {                     system.out.println("woops");                 }                   system.out.printf("exit next: " + counter + "\n");             }             if(action.equals("previous"))             {                 counter--;                 if (counter < 0) {counter = picture.length-1;}             string.format("resource/%s",picture[counter].getname());                  try                 {                     image = new imageicon("resource/"+picture[counter].getname());                     midlabel.seticon(image);                  }                 catch (exception xu)                 {                     system.out.println("woops");                 }                   system.out.printf("exit prev: " + counter + "\n");             }         }     } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -