java - How to use image slider in swing? -


hi trying use slide image using prev , next button button not able did following code achieve output when run program , try slide image shows image not available i. not getting why happening. found code website

   public class imageslider extends jpanel implements actionlistener {      private static final int max = 20;     private static final font sans = new font("sansserif", font.plain, 16);     private static final border border =         borderfactory.creatematteborder(4, 16, 4, 16, color.lightgray);     private list<string> list = new arraylist<string>(max);     private list<imageicon> cache = new arraylist<imageicon>(max);     private jlabel imagelabel = new jlabel();     private jbutton prevbutton = new jbutton();     private jbutton nextbutton = new jbutton();     private jcombobox favorites;      public imageslider() {         this.setlayout(new borderlayout());          list.add("e:\\software\\trainpis\\res\\drawable\\yellow.png");         list.add("e:\\software\\trainpis\\res\\drawable\\a0.png");         list.add("e:\\software\\trainpis\\res\\drawable\\yellow.png");          list.add("e:\\software\\trainpis\\res\\drawable\\a0.png");          (int = 0; < list.size(); i++) cache.add(i, null);          jlabel titlelabel = new jlabel();         titlelabel.settext("imageslider");         titlelabel.sethorizontalalignment(jlabel.center);         titlelabel.setfont(new font(font.sans_serif, font.bold, 24));         titlelabel.setborder(border);         this.add(titlelabel, borderlayout.north);          imagelabel.seticon(getimage(0));         imagelabel.sethorizontalalignment(jlabel.center);         imagelabel.setborder(border);         this.add(imagelabel, borderlayout.center);          favorites = new jcombobox(             list.toarray(new string[list.size()]));         favorites.setactioncommand("favs");         favorites.addactionlistener(this);          prevbutton.settext("\u22b2prev");         prevbutton.setfont(sans);         prevbutton.setactioncommand("prev");         prevbutton.addactionlistener(this);          nextbutton.settext("next\u22b3");         nextbutton.setfont(sans);         nextbutton.setactioncommand("next");         nextbutton.addactionlistener(this);          jpanel controlpanel = new jpanel();         controlpanel.add(prevbutton);         controlpanel.add(favorites);         controlpanel.add(nextbutton);         controlpanel.setborder(border);         this.add(controlpanel, borderlayout.south);     }      public void actionperformed(actionevent ae) {         string cmd = ae.getactioncommand();         if ("favs".equals(cmd)) {             int index = favorites.getselectedindex();             imageicon image = getimage(index);             imagelabel.seticon(image);             if (image != null) imagelabel.settext("");             else imagelabel.settext("image not available.");         }         if ("prev".equals(cmd)) {             int index = favorites.getselectedindex() - 1;             if (index < 0) index = list.size() - 1;             favorites.setselectedindex(index);         }         if ("next".equals(cmd)) {             int index = favorites.getselectedindex() + 1;             if (index > list.size() - 1) index = 0;             favorites.setselectedindex(index);         }     }      public jbutton getdefault() { return nextbutton; }      // return (possibly cached) image having given index.     private imageicon getimage(int index) {         imageicon image = cache.get(index);         if (image != null) return image;         string name = "images/" + list.get(index);         url url = imageslider.class.getresource(name);         if (url != null) {             image = new imageicon(url);         }         cache.set(index, image);         return image;     }      public static void main(string[] args) {         eventqueue.invokelater(new runnable() {             public void run() {                 jframe frame = new jframe();                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 imageslider go = new imageslider();                 frame.add(go);                 frame.settitle("imageslider");                 frame.setsize(400, 300);                 frame.setvisible(true);                 go.getdefault().requestfocusinwindow();             }         });     } } 

please tell me wrong in advance

" found code website"

copying ans pasting code never way go. learn how/why works, , create own code uses found code reference.

" try slide image shows image not available i."

without running program, because don't have image resources, see problem. code, uses getresource(), images in class path.

url url = imageslider.class.getresource(name); 

the path name passed requires class path reference. doing passing absolute path

list.add("e:\\software\\trainpis\\res\\drawable\\yellow.png"); ... string name = "images/" + list.get(index); 

the way code set up, looks producer of code had images folder inside src , passed/added image file name e.g. "image.png"

list.add("image.png"); 

you file structure should like

projectroot          src              images                   image.png 

note: educated guess. try out (change image location , paths. if doesn't work, try add / before "images/""/images/"


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -