java - Duplicating a JPanel - (Copy JPanel's static image to different JPanel) -


i'm pretty new swing , receive im stuck task.

current state:

im having nice jframe object (guiframe) having 2 jpanel on (tabspanel , cardpanel)(one simple jpanel buttons, other has cardlayout switched tabspanel buttons).

problem:

the task if press button "show" on tabspanel need send cardpanel different window (showframe) static "image", while on previous window program still running , nice. basicly im trying copy / clone cardpanel.

what have tried:

  • i have tried

    jpanel jpanelshow = cardpanel;  show.add(jpanelshow); 

    of course not working because reference number being copied , if run program, cardpanel "disappears".

  • i have tried use clone() working i'm getting weird nullpointerexception isn't caused code.

current codes (cloning try):

cardpanel.java

/**  * basicly jpanel, clone() implemented  */ package javaapplication5;  import javax.swing.jpanel; import java.util.stack;  public class cardpanel extends jpanel implements cloneable {      public cardpanel() {         super();     }      @override     public cardpanel clone() throws nullpointerexception {         /* creating return object */         final cardpanel copy;          try {              /* cloning */             copy = (cardpanel) super.clone();         } catch (clonenotsupportedexception e) {              /* exception (should not happen though) */             e.printstacktrace();             return null;         }         return copy;     } } 

cardlayoutexample.java

package javaapplication5;  import java.awt.borderlayout; import java.awt.cardlayout; import java.awt.color; import java.awt.container; import java.awt.eventqueue; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.borderfactory; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.border.border;  public class cardlayoutexample {       jframe guiframe;     cardlayout cards;     cardpanel cardpanel;     private int showframenotshownyet = 1;     public showframe show = new showframe();      public static void main(string[] args) {           /* random things swing */          eventqueue.invokelater(new runnable()          {                         @override              public void run()              {                       new cardlayoutexample();                       }          });      }      public cardlayoutexample()     {          /* creating main jframe */         guiframe = new jframe();          /* making sure program exits when frame closes */         guiframe.setdefaultcloseoperation(jframe.exit_on_close);         guiframe.settitle("cardlayout example");         guiframe.setsize(400,300);          /* center jframe in middle of screen */         guiframe.setlocationrelativeto(null);         guiframe.setlayout(new borderlayout());          /* border jpanel separation */         border outline = borderfactory.createlineborder(color.black);          /* creating jbutton1 tabspanel */         jbutton switchcards1 = new jbutton("1");         switchcards1.setactioncommand("1");         switchcards1.addactionlistener(new actionlistener()         {             @override             public void actionperformed(actionevent event)             {                 cards.show(cardpanel, "testcontent");                  }         });          /* creating jbutton2 tabspanel */         jbutton switchcards2 = new jbutton("2");         switchcards2.setactioncommand("2");         switchcards2.addactionlistener(new actionlistener()         {             @override             public void actionperformed(actionevent event)             {                 cards.show(cardpanel, "testcontent1");                   }         });          /* creating jbutton3 tabspanel */         jbutton switchcards3 = new jbutton("3");         switchcards3.setactioncommand("3");         switchcards3.addactionlistener(new actionlistener()         {             @override             public void actionperformed(actionevent event)             {                 cards.show(cardpanel, "testcontent2");                 }         });          jbutton switchcards4 = new jbutton("show");         switchcards4.setactioncommand("show");         switchcards4.addactionlistener(new actionlistener()         {             @override             public void actionperformed(actionevent event)             {                  /* if there no showframe yet */                 if(showframenotshownyet == 1){                      show.add((jpanel)cardpanel.clone());                     show.setvisible(true);                     showframenotshownyet = 0;                  }                   /* if there showframe */                 else {                     show.setvisible(false);                     showframenotshownyet = 1;                     guiframe.repaint();                 }                }         });          jbutton switchcards5 = new jbutton("refresh");         switchcards5.setactioncommand("refresh");         switchcards5.addactionlistener(new actionlistener()         {             @override             public void actionperformed(actionevent event)             {                                         show.setvisible(false);                     show.add((jpanel)cardpanel.clone());                     show.setvisible(true);                     showframenotshownyet = 0;             }         });          /* creating jpanel buttons */         jpanel tabspanel = new jpanel();         tabspanel.setborder(outline);         tabspanel.add(switchcards1);         tabspanel.add(switchcards2);         tabspanel.add(switchcards3);         tabspanel.add(switchcards4);         tabspanel.add(switchcards5);          /* creating jpanel cardlayout */         cards = new cardlayout();         cardpanel = new cardpanel();         cardpanel.setlayout(cards);         cards.show(cardpanel, "testcontent");          /* adding 1st card */         jpanel firstcard = new testcontent();         cardpanel.add(firstcard, "testcontent");          /* adding 2nd card */         jpanel secondcard = new testcontent1();         cardpanel.add(secondcard, "testcontent1");          /* adding 3rd card */         jpanel thirdcard = new testcontent2();         cardpanel.add(thirdcard, "testcontent2");          /* filling jframe stuff */         guiframe.add(tabspanel,borderlayout.north);         guiframe.add(cardpanel,borderlayout.center);         guiframe.setvisible(true);     }  } 

testcontent, testcontent1 , testcontent2 simple jpanels random stuff generated swinggui, showframe empty jframe. if needed paste in codes too.

if need "image" of cardpanel, can create image , use jlabel show, example...

bufferedimage img = new bufferedimage(cardpanel.getwidth(), cardpane.getheight(), bufferedimage.bufferedimage.type_int_rgb); graphics2d g2d = img.creategraphics(); cardpanel.printall(g2d); g2d.dispose(); 

now have "copy" of cardpanel, can use jlabel display it, example...

jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.dipose_on_close); frame.add(new jlabel(new imageicon(img))); frame.pack(); frame.setlocationrelativeto(this); frame.setvisible(true); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -