java - Repaint JPanel from different class -


i'm having problems repainting class after have added object it.

public class window extends jframe {  /**  *   */ private static final long serialversionuid = 1l;  private mediahandler mymediahandler = new mediahandler(this);  class menuactionlistener implements actionlistener {      @override     public void actionperformed(actionevent e) {         string buttontext = e.getactioncommand();         switch(buttontext) {         case "add movie":             addmoviewindow addmoviegui = new addmoviewindow(mymediahandler);             addmoviegui.setvisible(true);             break;         case "add tv-show":             addtvshowwindow addtvshowgui = new addtvshowwindow(mymediahandler);             addtvshowgui.setvisible(true);             break;         }     } }  public window() { //  mymediahandler.addmovie("nineteen eighty-four", 113.00, 1984, "michael radford", "mediapath", "https://dl.dropboxusercontent.com/u/16670644/projekt/1984.png", "hd", false, "eng", "george orwell (novel)");     addcomponents();     configurframe();     addmenu();      validate();     repaint(); }  private void addmenu() {     jmenubar bar = new jmenubar();     jmenu menu = new jmenu("menu");     bar.add(menu);      jmenuitem item = new jmenuitem("add movie");     item.addactionlistener(new menuactionlistener());     menu.add(item);      item = new jmenuitem("add tv-show");     item.addactionlistener(new menuactionlistener());     menu.add(item);      this.setjmenubar(bar); }  private void configurframe() {     this.setsize(1205, 850);     this.settitle("video library");     this.setlocationrelativeto(null);     this.setresizable(false);     this.setdefaultcloseoperation(windowconstants.exit_on_close);     this.seticonimage(new imageicon("e:\\dropbox\\dropbox\\programmering\\java\\projekt iv 1\\icon.png").getimage());      try {         java.net.url = new url("https://dl.dropboxusercontent.com/u/16670644/projekt/background.png");         imageicon image = new imageicon(where);         jlabel imagelabel = new jlabel(image);         this.add(imagelabel);     } catch (malformedurlexception e) {         // todo auto-generated catch block         system.out.println("error loading background makes background image");         e.printstacktrace();     } }  private void addcomponents() {     jpanel shelfs = buildshelfs();     this.add(shelfs); }  private jpanel buildshelfs() {     jpanel mediapanel = new jpanel();     int nrofmedias = mymediahandler.medialist.size();     mediapanel.setopaque(false);     mediapanel.setlayout(new gridlayout(3,6,22,30));     mediapanel.setbounds(90, 25, 1020, 745);              java.net.url where;             for(int = 0; < nrofmedias; i++) {                 string temp = mymediahandler.medialist.get(i).getimagepath();                 try {                     = new url(temp);                     imageicon image = new imageicon(where);                     jlabel imagelabel = new jlabel(image);                     mediapanel.add(imagelabel);                 } catch (malformedurlexception e) {                     // todo auto-generated catch block                     system.out.println("error loading user media picture");                     e.printstacktrace();                 }             }          java.net.url where1;         try {             for(int = nrofmedias; < 18; i++) {                 where1 = new url("https://dl.dropboxusercontent.com/u/16670644/projekt/temppic.png");                 imageicon image1 = new imageicon(where1);                            jlabel imagelabel1 = new jlabel(image1);                 mediapanel.add(imagelabel1);             }          } catch (malformedurlexception e) {             // todo auto-generated catch block             system.out.println("error loading temppic shows allowed media spots");             e.printstacktrace();         }          return mediapanel; }  public static void main(string[] arg) {     window mainwindowgui = new window();     mainwindowgui.setvisible(true); }    } 

what i'm trying class calling rapaint function.

public class mediahandler { arraylist<media> medialist;  private window mywindow;  public mediahandler(window window) {     this.medialist = new arraylist<media>();     mywindow = window; }  public void addmovie(string title, double playtime, int year,         string directory, string mediapath, string imagepath, string quality,         boolean subtitles, string language, string writer) {     medialist.add(new movie(title, playtime, year, false, directory, mediapath, imagepath, rating.unrated, quality, subtitles, language, writer));      mywindow.getcontentpane().invalidate();     mywindow.invalidate();  }    } 

please let me know if there unclear! have been stuck on problem few days now.

it's lot of code, , not cleanly structured , so. far can see, problem can not solved "repaining" anything. problem labels (showing media images) not added shelf.

you calling buildshelfs method once, when frame constructed. when later adding new movie via mediahandler, adding movie medialist. not adding new label/image mediapanel.

this can solved follows: can declare mediapanel instance variable. , can offer method add new imageicon panel, image read particular url (given string). method can on 1 hand used during initialization (that is, during first call buildshelfs). more importantly: can call method mediahandler, new imageicon added mediapanel whenever new movie added in moviehandler.

i tried sketch basic idea in following code. again, structure of current code not nice , clean, there guesswork involved....

class window ...  {     // store instance variable     private jpanel mediapanel;       private jpanel buildshelfs()     {         mediapanel = new jpanel();         mediapanel.setopaque(false);         mediapanel.setlayout(new gridlayout(3, 6, 22, 30));         mediapanel.setbounds(90, 25, 1020, 745);         (int i=0; i<18; i++)         {             addmedia(i);         }         return mediapanel;     }       private void addmedia(int index)     {         // should store these images locally!!!         string urlstring = "https://dl.dropboxusercontent.com/u/16670644/projekt/temppic.png";          int nrofmedias = mymediahandler.medialist.size();         if (index < nrofmedias)         {             urlstring = mymediahandler.medialist.get(i).getimagepath();         }         addmedia(urlstring);     }      void addmedia(string urlstring)     {         try         {             java.net.url = new url(urlstring);             imageicon image = new imageicon(where);             jlabel imagelabel = new jlabel(image);             mediapanel.add(imagelabel);             mediapanel.invalidate();             mediapanel.validate();         }         catch (malformedurlexception e)         {             system.out.println("error loading user media picture "+urlstring);             e.printstacktrace();         }     }  }     class mediahandler  {     arraylist<media> medialist;      private window mywindow;      public mediahandler(window window)      {         this.medialist = new arraylist<media>();         mywindow = window;     }      public void addmovie(string title, double playtime, int year,         string directory, string mediapath, string imagepath, string quality,         boolean subtitles, string language, string writer)      {         medialist.add(new movie(title, playtime, year,              false, directory, mediapath, imagepath, rating.unrated,              quality, subtitles, language, writer));          mywindow.addmedia(mediapath);     }    } 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -